I've been writing Java for the last couple of years , and now I've started to write in python (in addition).
我在过去几年里一直在编写Java,现在我已经开始用python编写了(另外)。
The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java.
问题在于,当我查看我的Python代码时,看起来有人试图将Java代码编成python格式,而且它很糟糕,因为 - 好吧,python不是Java。
Any tips on how to escape this pattern of "Writing Java in Python"?
关于如何摆脱“用Python编写Java”模式的任何提示?
Thanks!
13 个解决方案
#1
21
You might consider immersing yourself in the Python paradigms. The best way is to first know what they are then explore the best practices by reading some literature and reviewing some code samples. I recommend Learning Python by Mark Lutz; great for beginners and advanced users.
您可以考虑将自己沉浸在Python范例中。最好的方法是先了解它们是什么,然后通过阅读一些文献和审查一些代码示例来探索最佳实践。我推荐Mark Lutz学习Python;非常适合初学者和高级用户。
You'll do yourself a great injustice if you program with Python and fail to leverage all of the built-in, developer-friendly, Pythonic syntax.
如果您使用Python编程并且未能利用所有内置的,开发人员友好的Pythonic语法,那么您将自己做一个非常不公平的事情。
As my French teacher used to say, "French isn't just English with different words."
正如我的法语老师曾经说过的那样,“法语不仅仅是英语,还有不同的词汇。”
#2
6
You could start by reading The Zen of Python. It'll give you some insight into how Python code is supposed to be written, provided you understand the language enough to understand what it's talking about. :-)
你可以从阅读The Zen of Python开始。它会让你深入了解如何编写Python代码,只要你理解了足以理解它所谈论内容的语言。 :-)
#3
6
If you are new to Python and coming from Java (or C#, or other similar statically typed OO language), these classic articles from PJ Eby and Ryan Tomayko are necessary reading:
如果您是Python新手并且来自Java(或C#或其他类似的静态类型OO语言),那么来自PJ Eby和Ryan Tomayko的这些经典文章是必读的:
- Python Is Not Java (PJE)
- Java is not Python, either (PJE)
- Python Interfaces are not Java Interfaces (PJE)
- The Static Method Thing (Tomayko)
- Getters/Setters/Fuxors (Tomayko)
Python不是Java(PJE)
Java也不是Python(PJE)
Python接口不是Java接口(PJE)
静态方法(Tomayko)
#4
5
Some of the major ways in which Python differs from C/Java-like languages are:
Python与C / Java类语言不同的一些主要方式是:
List comprehensions.
-
Support for functional programming.
支持函数式编程。
-
The use of certain Pythonic constructs instead of similar C-like constructs although both seem to work (list comprehensions can be argued to be a part of this, but there are others).
使用某些Pythonic结构而不是类似的C样结构虽然两者似乎都有用(列表推导可以被认为是其中的一部分,但还有其他的)。
There are others, but these are the main ones that bugged me when I first started Python (and I had come from years of Java like you).
还有其他一些,但是这些是我第一次开始使用Python时遇到的主要问题(而且我来自像你这样多年的Java)。
Before using any of these, it is helpful to understand why you should go for pythonic code rather than the usual C/Java way in Python, although both give you the same output.
在使用其中任何一个之前,理解为什么你应该使用pythonic代码而不是Python中常用的C / Java方法是有帮助的,尽管两者都给你相同的输出。
For starters, Python provides some powerful features not available in C/Java that makes your code much clearer and simpler (although this is subjective, and might not look any better to someone coming from Java at first). The first two points fall into this category. For example, support for functions as first class objects and closures makes it easy to do things that would need all kinds of weird acrobatics with inner classes in Java.
对于初学者来说,Python提供了一些C / Java中没有的强大功能,这些功能使您的代码更加清晰和简单(尽管这是主观的,对于最初来自Java的人来说可能看起来不那么好)。前两点属于这一类。例如,对作为第一类对象和闭包的函数的支持使得在Java中使用内部类需要各种奇怪的杂技的事情变得容易。
But a major reason is that Python is an interpreted language, and certain constructs are much faster than the equivalent C/Java-like code. For example, list comprehensions are usually a lot faster than an equivalent for-loop that iterates over the indices of a list and accesses each item by index. This is a very objective benefit, and IMHO a lot of the "Python in way too slow" way of thinking comes from using Java-style code shoe-horned into Python.
但一个主要原因是Python是一种解释型语言,某些构造比同等的C / Java代码快得多。例如,列表推导通常比等价的for循环快得多,循环遍历列表的索引并按索引访问每个项目。这是一个非常客观的好处,而恕我直言的很多“Python太慢”的思维方式来自于使用Java风格的代码。
One of the best ways to learn about pythonic code is to read other people's code. I actually learnt a lot by looking at Python code posted in answers to SO questions. These often come with explanations and it is usually obvious why it is better than non-pythonic code (speed, clarity, etc.).
了解pythonic代码的最佳方法之一是阅读其他人的代码。通过查看SO问题答案中发布的Python代码,我实际上学到了很多东西。这些通常带有解释,并且通常很明显为什么它比非pythonic代码(速度,清晰度等)更好。
Edit:
Of course, there are other ways of getting other people's code. You can also download and look through the code of any good open source Python project. Books are also a good resource, I would recommend O'Reilly Python Cookbook. It has lots of useful code examples and very detailed explanations.
当然,还有其他方法可以获取其他人的代码。您还可以下载并查看任何优秀的开源Python项目的代码。书籍也是一个很好的资源,我会推荐O'Reilly Python Cookbook。它有许多有用的代码示例和非常详细的解释。
#5
4
If you want to see some fairly idiomatic Python that does non-trivial stuff, there's Dive Into Python, although Dive Into Python 3 is newer and might be a better source of style tips. If you're looking more for some points to review, there's Code Like a Pythonista.
如果你想看到一些相当惯用的Python做一些非常重要的事情,那就是Dive Into Python,虽然Dive Into Python 3更新,可能是更好的风格提示源。如果您正在寻找更多要点的评论,那么Code就像Pythonista一样。
#6
3
You could post your code at Refactor my code to see if someone can show you a more Pythonic way to do it.
您可以在Refactor我的代码中发布您的代码,看看是否有人可以向您展示更多的Pythonic方法。
#7
3
Definitely not a panacea but I think you should try some code golf in Python. Obviously nobody should write "golfed" code IRL, but finding the most terse way to express something really forces you to exploit the built in functionality of the language.
绝对不是灵丹妙药,但我认为你应该在Python中尝试一些代码高尔夫。显然,没有人应该编写“高尔夫”代码IRL,但找到最简洁的表达方式真的会迫使你利用语言的内置功能。
#8
2
Someone provided me with this list of how "Python is not Java" when I started Python after Java, and it was very helpful.
当我在Java之后启动Python时,有人向我提供了“Python不是Java”的列表,这非常有用。
Also, check out this similar SO question that I posted a short time ago when in a similar position.
另外,请查看我在不久之前在类似位置发布的类似SO问题。
#9
1
Try to find algorithms that you understand well and see how they are implemented in python standard libraries.
尝试找到您理解的算法,并了解它们是如何在python标准库中实现的。
Persist. :)
#10
0
Learn a few other languages. It will help you make the difference between algorithms (the structure of processing, unchanged between languages) and the local syntaxic features of the language. Then you can "write Foo in Bar" for any combination of languages "Foo" and "Bar".
学习一些其他语言。它将帮助您区分算法(处理结构,语言之间不变)和语言的本地语法特征。然后,您可以“为Foo”和“Bar”语言组合“写入Foo in Bar”。
#11
0
Eat Python, Sleep Python and Drink Python. That is the only way........
吃Python,睡眠Python和喝Python。那是唯一的方法........
#12
0
1) Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts.
1)Python支持面向对象编程的许多(但不是全部)方面;但是可以在不使用OO概念的情况下编写Python程序。
1) Java supports only object-oriented programming.
1)Java仅支持面向对象的编程。
2) Python is designed to be used interpretively. A Python statement may be entered at the interpreter prompt
2)Python旨在解释性地使用。可以在解释器提示符处输入Python语句
(>>>)
, and will be executed immediately. (Implementations make some use of automatic compilation into bytecodes (.pyc files).
,并将立即执行。 (实现使用自动编译为字节码(.pyc文件)。
2) Programs written in Java must be explicitly compiled into bytecodes (.class files), though an IDE may do this automatically in a way that is transparent to the user. Java does not support direct execution of statements - though there are tools like Dr. Java that support this.
2)用Java编写的程序必须显式编译为字节码(.class文件),尽管IDE可以以对用户透明的方式自动执行此操作。 Java不支持直接执行语句 - 尽管有像Dr. Java这样的工具支持这一点。
3) Python is dynamically typed: • A variable is introduced by assigning a value to it. Example:
3)Python是动态类型的:•通过为其赋值来引入变量。例:
someVariable = 42
someVariable = 42
• A variable that has been assigned a value of a given type may later be assigned a value of a different type. Example:
•已分配给定类型值的变量稍后可能会被分配不同类型的值。例:
someVariable = 42
someVariable = 'Hello, world'
3) Java is
3)Java是
statically typed
: • A variable must be explicitly declared to be of some type before assigning a value to it, though declaration and assignment may be done at the same time. Examples:
:•在为其赋值之前,必须将变量显式声明为某种类型,尽管声明和赋值可以同时完成。例子:
int someVariable;
int someVariable = 42;
• A variable that has been declared to be of a particular type may not be assigned a value of a different type.
•已声明为特定类型的变量可能未分配不同类型的值。
4) Python supports the following built-in data types: Plain integers (normally 32-bit integers in the range -2147483648 through 2147483647). • Long integers (size limited only by memory size of the machine running on) • Booleans (False and True). • Real numbers. • Complex numbers. In addition, Python supports a number of types that represent a collection of values - including strings, lists, and dictionaries.
4)Python支持以下内置数据类型:普通整数(通常为-2147483648到2147483647范围内的32位整数)。 •长整数(大小仅受运行机器的内存大小限制)•布尔值(假和真)。 •实数。 •复数。此外,Python支持许多表示值集合的类型 - 包括字符串,列表和字典。
4) Java has two kinds of data types: primitive types and reference types. Java supports the following primitive data types: • byte - 8-bit integers • short - 16-bit integers • int - 32-bit integers • long - 64-bit integers (Java also supports a class java.math.BigInteger to represent integers whose size is limited only by memory) • float - 32-bit real numbers. • double - 32-bit real numbers. • boolean - (false and true). • char - a single character. In addition, Java supports arrays of any type as the reference types, and the API includes the class String and a large number of classes used for collections of values.
4)Java有两种数据类型:基本类型和引用类型。 Java支持以下原始数据类型:•byte - 8位整数•short - 16位整数•int - 32位整数•long - 64位整数(Java还支持类java.math.BigInteger来表示整数其大小仅受内存限制)•float - 32位实数。 •双 - 32位实数。 •布尔值 - (假和真)。 •char - 单个字符。此外,Java支持任何类型的数组作为引用类型,API包括String类和用于值集合的大量类。
5)
Python is line-oriented:
Python是面向行的:
statements end at the end of a line unless the line break is explicitly escaped with . There is no way to put more than one statement on a single line. Examples: this is a statement this is another statement this is a long statement that extends over more \ than one line
语句在行的末尾结束,除非换行符显式转义。没有办法在一行上放置多个语句。示例:这是一个声明,这是另一个声明,这是一个延伸超过一行的长语句
5)
Statements in Java always end with a
semicolon (;)
. It is possible for a statement to run over more than one line, or to have multiple statements on a single line. Examples: this is a statement; this is another statement; this is a long statement that extends over more than one line; a statement; another; another;
。语句可以在多行上运行,或者在一行上有多个语句。示例:这是一个声明;这是另一个声明;这是一个冗长的陈述,延伸超过一行;一份声明;另一个;另一个;
6)
Python comments begin with #
Python评论以#开头
and extend to the end of the line. Example:
并延伸到该行的末尾。例:
This is a comment
A new statement starts here
一个新的声明从这里开始
6) Java has two kinds of comments. A comment beginning with // extend to the end of the line (like Python comments). Comments can also begin with /* and end with */. These can extend over multiple lines or be embedded within a single line. Examples:
6)Java有两种注释。以//开头的注释扩展到行的末尾(如Python注释)。注释也可以以/ *开头,以* /结尾。这些可以扩展到多条线路或嵌入一条线路中。例子:
// This is a comment
A new statement starts here
/* This is also a comment */
/* And this is also a comment, which is
long enough to require several lines
to say it. */
Statement starts /* comment */ then continues
声明开始/ *评论* /然后继续
7) Python strings can be enclosed in either single or double quotes (' or ""). A character is represented by a string of length 1. Examples:
7)Python字符串可以用单引号或双引号括起来('或“”)。字符由长度为1的字符串表示。示例:
'This is a string'
"This is also a string" # Equivalent
'c' # A string
"c" # An equivalent string
Python uses the following operators for constructing compound boolean expressions:
Python使用以下运算符来构造复合布尔表达式:
and, or and not. Example:
not(x > 0 and y > 0) or z > 0
7) Java strings must be enclosed in double quotes (""). A character is a different type of object and is enclosed in single quotes ('). Examples:
7)Java字符串必须用双引号(“”)括起来。字符是不同类型的对象,并用单引号(')括起来。例子:
"This is a String"
'c' // A character, but not a String
Java uses the following operators for
constructing compound boolean expressions:
&&, ||, ! and ^ (meaning exclusive or)
Example:
! (x > 0 && y > 0) || z > 0 ^ w > 0
8) In Python, the comparison operators
8)在Python中,比较运算符
(>, <, >=, <=, == and !=) can be applied to numbers
(>,<,> =,<=,==和!=)可以应用于数字
, strings, and other types of objects), and compare values in some appropriate way (e.g. numeric order, lexical order) where possible.
,字符串和其他类型的对象),并在可能的情况下以某种适当的方式(例如数字顺序,词汇顺序)比较值。
8) In Java, most of the comparison operators ( >, <, >=, and <=) can be applied only to primitive types. Two (== and !=) can be applied to any object, but when applied to reference types they test for same (different) object rather than same (different) value.
8)在Java中,大多数比较运算符(>,<,> =和<=)只能应用于基本类型。两个(==和!=)可以应用于任何对象,但是当应用于引用类型时,它们测试相同(不同)对象而不是相同(不同)值。
9) There is no universally-accepted Python convention for naming classes, variables, functions etc.
9)没有普遍接受的Python约定来命名类,变量,函数等。
9) By convention, most names in Java use mixed case. Class names begin with an uppercase letter; variable and function names begin with a lowercase letter. Class constants are named using all uppercase letters with underscores. Examples: AClassName aVariableName aFunctionName() A_CLASS_CONSTANT
9)按照惯例,Java中的大多数名称都使用混合大小写。类名以大写字母开头;变量和函数名称以小写字母开头。类常量使用带下划线的全部大写字母命名。示例:AClassName aVariableName aFunctionName()A_CLASS_CONSTANT
10) Python definite looping statements have the form for variable in expression: Example:
10)Python定义循环语句在表达式中具有变量的形式:示例:
for p in pixels:
something
10) Java has two kinds of definite looping statements. One has the form
10)Java有两种明确的循环语句。一个有形式
for (variable in collection) Example:
for (p in pixels)
something;
11) Python uses the built-in function range() with for to loop over a range of integers. Examples:
11)Python使用内置函数range()和for循环一系列整数。例子:
for i in range(1, 10)
something
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for i in range(1, 10, 2)
something
(i takes on values 1, 3, 5, 7, 9)
11) Java uses a different form of the for to loop over a range of integers. Examples:
11)Java使用不同形式的for来遍历一系列整数。例子:
for (int i = 1; i < 10; i ++)
something;
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for (int i = 1; i < 10; i += 2)
something;
(i takes on values 1, 3, 5, 7, 9)
12) Python conditional statements have the form if condition: and an optional else part has the form else:. The form elif condition: is allowed as an alternative to an else: immediately followed by an if. Examples:
12)Python条件语句具有if条件的形式:并且可选的else部分具有以下形式:形式elif条件:允许作为else的替代:紧接着是if。例子:
if x < 0:
something
if x < 0:
something
else:
something different
if x < 0:
something
elif x > 0:
something different
else:
yet another thing
12) Java conditional statements have the form if (condition) and an optional else part has the form else (no colon) There is no elif form - else if is used directly. Examples:
12)Java条件语句的形式为if(condition),另一个else部分的形式为else(无冒号)没有elif形式 - 否则直接使用。例子:
if (x < 0)
something;
if (x < 0)
something;
else
something different;
if (x < 0)
something;
else if (x > 0)
something different;
else
yet another thing;
13) The scope of a Python conditional or looping statement is denoted by indentation. (If multiple lines are to be included, care must be used to be sure every line is indented identically). Examples:
13)Python条件或循环语句的范围由缩进表示。 (如果要包含多行,必须注意确保每一行都是相同的缩进)。例子:
if x < 0:
do something
do another thing regardless of the value of x
if x < 0:
do something
do something else
do yet a third thing
do another thing regardless of the value of x
13) The scope of a Java conditional or looping statement is normally just the next statement. Indentation is ignored by the compiler (though stylistically it is still highly desirable for the benefit of a human reader). If multiple lines are to be included, the scope must be delimited by curly braces ({ , }). (Optionally, these can be used even if the scope is a single line.) Examples:
13)Java条件语句或循环语句的范围通常只是下一个语句。编译器忽略缩进(虽然在风格上它仍然非常需要人类读者的利益)。如果要包含多行,则范围必须用花括号({,})分隔。 (可选,即使范围是单行,也可以使用这些。)示例:
if (x < 0)
do something;
do another thing regardless of the value of x;
if (x < 0)
do something; // Bad style-don't do this!
do another thing regardless of the value of x;
if (x < 0)
{
do something;
do something else;
do yet a third thing;
}
do another thing regardless of the value of x;
if (x < 0)
{
do something;
}
do another thing regardless of the value of x;
#13
-2
This is useful if you want to understand how to code to python in a more pythonic or correct way: http://www.python.org/dev/peps/pep-0008/
如果您想了解如何以更加pythonic或更正确的方式编写python代码,这非常有用:http://www.python.org/dev/peps/pep-0008/
#1
21
You might consider immersing yourself in the Python paradigms. The best way is to first know what they are then explore the best practices by reading some literature and reviewing some code samples. I recommend Learning Python by Mark Lutz; great for beginners and advanced users.
您可以考虑将自己沉浸在Python范例中。最好的方法是先了解它们是什么,然后通过阅读一些文献和审查一些代码示例来探索最佳实践。我推荐Mark Lutz学习Python;非常适合初学者和高级用户。
You'll do yourself a great injustice if you program with Python and fail to leverage all of the built-in, developer-friendly, Pythonic syntax.
如果您使用Python编程并且未能利用所有内置的,开发人员友好的Pythonic语法,那么您将自己做一个非常不公平的事情。
As my French teacher used to say, "French isn't just English with different words."
正如我的法语老师曾经说过的那样,“法语不仅仅是英语,还有不同的词汇。”
#2
6
You could start by reading The Zen of Python. It'll give you some insight into how Python code is supposed to be written, provided you understand the language enough to understand what it's talking about. :-)
你可以从阅读The Zen of Python开始。它会让你深入了解如何编写Python代码,只要你理解了足以理解它所谈论内容的语言。 :-)
#3
6
If you are new to Python and coming from Java (or C#, or other similar statically typed OO language), these classic articles from PJ Eby and Ryan Tomayko are necessary reading:
如果您是Python新手并且来自Java(或C#或其他类似的静态类型OO语言),那么来自PJ Eby和Ryan Tomayko的这些经典文章是必读的:
- Python Is Not Java (PJE)
- Java is not Python, either (PJE)
- Python Interfaces are not Java Interfaces (PJE)
- The Static Method Thing (Tomayko)
- Getters/Setters/Fuxors (Tomayko)
Python不是Java(PJE)
Java也不是Python(PJE)
Python接口不是Java接口(PJE)
静态方法(Tomayko)
#4
5
Some of the major ways in which Python differs from C/Java-like languages are:
Python与C / Java类语言不同的一些主要方式是:
List comprehensions.
-
Support for functional programming.
支持函数式编程。
-
The use of certain Pythonic constructs instead of similar C-like constructs although both seem to work (list comprehensions can be argued to be a part of this, but there are others).
使用某些Pythonic结构而不是类似的C样结构虽然两者似乎都有用(列表推导可以被认为是其中的一部分,但还有其他的)。
There are others, but these are the main ones that bugged me when I first started Python (and I had come from years of Java like you).
还有其他一些,但是这些是我第一次开始使用Python时遇到的主要问题(而且我来自像你这样多年的Java)。
Before using any of these, it is helpful to understand why you should go for pythonic code rather than the usual C/Java way in Python, although both give you the same output.
在使用其中任何一个之前,理解为什么你应该使用pythonic代码而不是Python中常用的C / Java方法是有帮助的,尽管两者都给你相同的输出。
For starters, Python provides some powerful features not available in C/Java that makes your code much clearer and simpler (although this is subjective, and might not look any better to someone coming from Java at first). The first two points fall into this category. For example, support for functions as first class objects and closures makes it easy to do things that would need all kinds of weird acrobatics with inner classes in Java.
对于初学者来说,Python提供了一些C / Java中没有的强大功能,这些功能使您的代码更加清晰和简单(尽管这是主观的,对于最初来自Java的人来说可能看起来不那么好)。前两点属于这一类。例如,对作为第一类对象和闭包的函数的支持使得在Java中使用内部类需要各种奇怪的杂技的事情变得容易。
But a major reason is that Python is an interpreted language, and certain constructs are much faster than the equivalent C/Java-like code. For example, list comprehensions are usually a lot faster than an equivalent for-loop that iterates over the indices of a list and accesses each item by index. This is a very objective benefit, and IMHO a lot of the "Python in way too slow" way of thinking comes from using Java-style code shoe-horned into Python.
但一个主要原因是Python是一种解释型语言,某些构造比同等的C / Java代码快得多。例如,列表推导通常比等价的for循环快得多,循环遍历列表的索引并按索引访问每个项目。这是一个非常客观的好处,而恕我直言的很多“Python太慢”的思维方式来自于使用Java风格的代码。
One of the best ways to learn about pythonic code is to read other people's code. I actually learnt a lot by looking at Python code posted in answers to SO questions. These often come with explanations and it is usually obvious why it is better than non-pythonic code (speed, clarity, etc.).
了解pythonic代码的最佳方法之一是阅读其他人的代码。通过查看SO问题答案中发布的Python代码,我实际上学到了很多东西。这些通常带有解释,并且通常很明显为什么它比非pythonic代码(速度,清晰度等)更好。
Edit:
Of course, there are other ways of getting other people's code. You can also download and look through the code of any good open source Python project. Books are also a good resource, I would recommend O'Reilly Python Cookbook. It has lots of useful code examples and very detailed explanations.
当然,还有其他方法可以获取其他人的代码。您还可以下载并查看任何优秀的开源Python项目的代码。书籍也是一个很好的资源,我会推荐O'Reilly Python Cookbook。它有许多有用的代码示例和非常详细的解释。
#5
4
If you want to see some fairly idiomatic Python that does non-trivial stuff, there's Dive Into Python, although Dive Into Python 3 is newer and might be a better source of style tips. If you're looking more for some points to review, there's Code Like a Pythonista.
如果你想看到一些相当惯用的Python做一些非常重要的事情,那就是Dive Into Python,虽然Dive Into Python 3更新,可能是更好的风格提示源。如果您正在寻找更多要点的评论,那么Code就像Pythonista一样。
#6
3
You could post your code at Refactor my code to see if someone can show you a more Pythonic way to do it.
您可以在Refactor我的代码中发布您的代码,看看是否有人可以向您展示更多的Pythonic方法。
#7
3
Definitely not a panacea but I think you should try some code golf in Python. Obviously nobody should write "golfed" code IRL, but finding the most terse way to express something really forces you to exploit the built in functionality of the language.
绝对不是灵丹妙药,但我认为你应该在Python中尝试一些代码高尔夫。显然,没有人应该编写“高尔夫”代码IRL,但找到最简洁的表达方式真的会迫使你利用语言的内置功能。
#8
2
Someone provided me with this list of how "Python is not Java" when I started Python after Java, and it was very helpful.
当我在Java之后启动Python时,有人向我提供了“Python不是Java”的列表,这非常有用。
Also, check out this similar SO question that I posted a short time ago when in a similar position.
另外,请查看我在不久之前在类似位置发布的类似SO问题。
#9
1
Try to find algorithms that you understand well and see how they are implemented in python standard libraries.
尝试找到您理解的算法,并了解它们是如何在python标准库中实现的。
Persist. :)
#10
0
Learn a few other languages. It will help you make the difference between algorithms (the structure of processing, unchanged between languages) and the local syntaxic features of the language. Then you can "write Foo in Bar" for any combination of languages "Foo" and "Bar".
学习一些其他语言。它将帮助您区分算法(处理结构,语言之间不变)和语言的本地语法特征。然后,您可以“为Foo”和“Bar”语言组合“写入Foo in Bar”。
#11
0
Eat Python, Sleep Python and Drink Python. That is the only way........
吃Python,睡眠Python和喝Python。那是唯一的方法........
#12
0
1) Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts.
1)Python支持面向对象编程的许多(但不是全部)方面;但是可以在不使用OO概念的情况下编写Python程序。
1) Java supports only object-oriented programming.
1)Java仅支持面向对象的编程。
2) Python is designed to be used interpretively. A Python statement may be entered at the interpreter prompt
2)Python旨在解释性地使用。可以在解释器提示符处输入Python语句
(>>>)
, and will be executed immediately. (Implementations make some use of automatic compilation into bytecodes (.pyc files).
,并将立即执行。 (实现使用自动编译为字节码(.pyc文件)。
2) Programs written in Java must be explicitly compiled into bytecodes (.class files), though an IDE may do this automatically in a way that is transparent to the user. Java does not support direct execution of statements - though there are tools like Dr. Java that support this.
2)用Java编写的程序必须显式编译为字节码(.class文件),尽管IDE可以以对用户透明的方式自动执行此操作。 Java不支持直接执行语句 - 尽管有像Dr. Java这样的工具支持这一点。
3) Python is dynamically typed: • A variable is introduced by assigning a value to it. Example:
3)Python是动态类型的:•通过为其赋值来引入变量。例:
someVariable = 42
someVariable = 42
• A variable that has been assigned a value of a given type may later be assigned a value of a different type. Example:
•已分配给定类型值的变量稍后可能会被分配不同类型的值。例:
someVariable = 42
someVariable = 'Hello, world'
3) Java is
3)Java是
statically typed
: • A variable must be explicitly declared to be of some type before assigning a value to it, though declaration and assignment may be done at the same time. Examples:
:•在为其赋值之前,必须将变量显式声明为某种类型,尽管声明和赋值可以同时完成。例子:
int someVariable;
int someVariable = 42;
• A variable that has been declared to be of a particular type may not be assigned a value of a different type.
•已声明为特定类型的变量可能未分配不同类型的值。
4) Python supports the following built-in data types: Plain integers (normally 32-bit integers in the range -2147483648 through 2147483647). • Long integers (size limited only by memory size of the machine running on) • Booleans (False and True). • Real numbers. • Complex numbers. In addition, Python supports a number of types that represent a collection of values - including strings, lists, and dictionaries.
4)Python支持以下内置数据类型:普通整数(通常为-2147483648到2147483647范围内的32位整数)。 •长整数(大小仅受运行机器的内存大小限制)•布尔值(假和真)。 •实数。 •复数。此外,Python支持许多表示值集合的类型 - 包括字符串,列表和字典。
4) Java has two kinds of data types: primitive types and reference types. Java supports the following primitive data types: • byte - 8-bit integers • short - 16-bit integers • int - 32-bit integers • long - 64-bit integers (Java also supports a class java.math.BigInteger to represent integers whose size is limited only by memory) • float - 32-bit real numbers. • double - 32-bit real numbers. • boolean - (false and true). • char - a single character. In addition, Java supports arrays of any type as the reference types, and the API includes the class String and a large number of classes used for collections of values.
4)Java有两种数据类型:基本类型和引用类型。 Java支持以下原始数据类型:•byte - 8位整数•short - 16位整数•int - 32位整数•long - 64位整数(Java还支持类java.math.BigInteger来表示整数其大小仅受内存限制)•float - 32位实数。 •双 - 32位实数。 •布尔值 - (假和真)。 •char - 单个字符。此外,Java支持任何类型的数组作为引用类型,API包括String类和用于值集合的大量类。
5)
Python is line-oriented:
Python是面向行的:
statements end at the end of a line unless the line break is explicitly escaped with . There is no way to put more than one statement on a single line. Examples: this is a statement this is another statement this is a long statement that extends over more \ than one line
语句在行的末尾结束,除非换行符显式转义。没有办法在一行上放置多个语句。示例:这是一个声明,这是另一个声明,这是一个延伸超过一行的长语句
5)
Statements in Java always end with a
semicolon (;)
. It is possible for a statement to run over more than one line, or to have multiple statements on a single line. Examples: this is a statement; this is another statement; this is a long statement that extends over more than one line; a statement; another; another;
。语句可以在多行上运行,或者在一行上有多个语句。示例:这是一个声明;这是另一个声明;这是一个冗长的陈述,延伸超过一行;一份声明;另一个;另一个;
6)
Python comments begin with #
Python评论以#开头
and extend to the end of the line. Example:
并延伸到该行的末尾。例:
This is a comment
A new statement starts here
一个新的声明从这里开始
6) Java has two kinds of comments. A comment beginning with // extend to the end of the line (like Python comments). Comments can also begin with /* and end with */. These can extend over multiple lines or be embedded within a single line. Examples:
6)Java有两种注释。以//开头的注释扩展到行的末尾(如Python注释)。注释也可以以/ *开头,以* /结尾。这些可以扩展到多条线路或嵌入一条线路中。例子:
// This is a comment
A new statement starts here
/* This is also a comment */
/* And this is also a comment, which is
long enough to require several lines
to say it. */
Statement starts /* comment */ then continues
声明开始/ *评论* /然后继续
7) Python strings can be enclosed in either single or double quotes (' or ""). A character is represented by a string of length 1. Examples:
7)Python字符串可以用单引号或双引号括起来('或“”)。字符由长度为1的字符串表示。示例:
'This is a string'
"This is also a string" # Equivalent
'c' # A string
"c" # An equivalent string
Python uses the following operators for constructing compound boolean expressions:
Python使用以下运算符来构造复合布尔表达式:
and, or and not. Example:
not(x > 0 and y > 0) or z > 0
7) Java strings must be enclosed in double quotes (""). A character is a different type of object and is enclosed in single quotes ('). Examples:
7)Java字符串必须用双引号(“”)括起来。字符是不同类型的对象,并用单引号(')括起来。例子:
"This is a String"
'c' // A character, but not a String
Java uses the following operators for
constructing compound boolean expressions:
&&, ||, ! and ^ (meaning exclusive or)
Example:
! (x > 0 && y > 0) || z > 0 ^ w > 0
8) In Python, the comparison operators
8)在Python中,比较运算符
(>, <, >=, <=, == and !=) can be applied to numbers
(>,<,> =,<=,==和!=)可以应用于数字
, strings, and other types of objects), and compare values in some appropriate way (e.g. numeric order, lexical order) where possible.
,字符串和其他类型的对象),并在可能的情况下以某种适当的方式(例如数字顺序,词汇顺序)比较值。
8) In Java, most of the comparison operators ( >, <, >=, and <=) can be applied only to primitive types. Two (== and !=) can be applied to any object, but when applied to reference types they test for same (different) object rather than same (different) value.
8)在Java中,大多数比较运算符(>,<,> =和<=)只能应用于基本类型。两个(==和!=)可以应用于任何对象,但是当应用于引用类型时,它们测试相同(不同)对象而不是相同(不同)值。
9) There is no universally-accepted Python convention for naming classes, variables, functions etc.
9)没有普遍接受的Python约定来命名类,变量,函数等。
9) By convention, most names in Java use mixed case. Class names begin with an uppercase letter; variable and function names begin with a lowercase letter. Class constants are named using all uppercase letters with underscores. Examples: AClassName aVariableName aFunctionName() A_CLASS_CONSTANT
9)按照惯例,Java中的大多数名称都使用混合大小写。类名以大写字母开头;变量和函数名称以小写字母开头。类常量使用带下划线的全部大写字母命名。示例:AClassName aVariableName aFunctionName()A_CLASS_CONSTANT
10) Python definite looping statements have the form for variable in expression: Example:
10)Python定义循环语句在表达式中具有变量的形式:示例:
for p in pixels:
something
10) Java has two kinds of definite looping statements. One has the form
10)Java有两种明确的循环语句。一个有形式
for (variable in collection) Example:
for (p in pixels)
something;
11) Python uses the built-in function range() with for to loop over a range of integers. Examples:
11)Python使用内置函数range()和for循环一系列整数。例子:
for i in range(1, 10)
something
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for i in range(1, 10, 2)
something
(i takes on values 1, 3, 5, 7, 9)
11) Java uses a different form of the for to loop over a range of integers. Examples:
11)Java使用不同形式的for来遍历一系列整数。例子:
for (int i = 1; i < 10; i ++)
something;
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for (int i = 1; i < 10; i += 2)
something;
(i takes on values 1, 3, 5, 7, 9)
12) Python conditional statements have the form if condition: and an optional else part has the form else:. The form elif condition: is allowed as an alternative to an else: immediately followed by an if. Examples:
12)Python条件语句具有if条件的形式:并且可选的else部分具有以下形式:形式elif条件:允许作为else的替代:紧接着是if。例子:
if x < 0:
something
if x < 0:
something
else:
something different
if x < 0:
something
elif x > 0:
something different
else:
yet another thing
12) Java conditional statements have the form if (condition) and an optional else part has the form else (no colon) There is no elif form - else if is used directly. Examples:
12)Java条件语句的形式为if(condition),另一个else部分的形式为else(无冒号)没有elif形式 - 否则直接使用。例子:
if (x < 0)
something;
if (x < 0)
something;
else
something different;
if (x < 0)
something;
else if (x > 0)
something different;
else
yet another thing;
13) The scope of a Python conditional or looping statement is denoted by indentation. (If multiple lines are to be included, care must be used to be sure every line is indented identically). Examples:
13)Python条件或循环语句的范围由缩进表示。 (如果要包含多行,必须注意确保每一行都是相同的缩进)。例子:
if x < 0:
do something
do another thing regardless of the value of x
if x < 0:
do something
do something else
do yet a third thing
do another thing regardless of the value of x
13) The scope of a Java conditional or looping statement is normally just the next statement. Indentation is ignored by the compiler (though stylistically it is still highly desirable for the benefit of a human reader). If multiple lines are to be included, the scope must be delimited by curly braces ({ , }). (Optionally, these can be used even if the scope is a single line.) Examples:
13)Java条件语句或循环语句的范围通常只是下一个语句。编译器忽略缩进(虽然在风格上它仍然非常需要人类读者的利益)。如果要包含多行,则范围必须用花括号({,})分隔。 (可选,即使范围是单行,也可以使用这些。)示例:
if (x < 0)
do something;
do another thing regardless of the value of x;
if (x < 0)
do something; // Bad style-don't do this!
do another thing regardless of the value of x;
if (x < 0)
{
do something;
do something else;
do yet a third thing;
}
do another thing regardless of the value of x;
if (x < 0)
{
do something;
}
do another thing regardless of the value of x;
#13
-2
This is useful if you want to understand how to code to python in a more pythonic or correct way: http://www.python.org/dev/peps/pep-0008/
如果您想了解如何以更加pythonic或更正确的方式编写python代码,这非常有用:http://www.python.org/dev/peps/pep-0008/