See http://dirtsimple.org/2004/12/python-is-not-java.html
Python is not Java below briefly record some of the ideas mentioned by the author 1. do not convert static methods in Java to class methods in Python (Note: methods defined in classes without the self parameter ). although the functions are the same, the class methods in Python are used to do some effects that cannot be implemented or are difficult to implement in Java (for example, inherit non-default constructor) A good solution is to implement the static method in Java in pthon as the module-level function (note, the module-level global function, right ?) The static final variable in Java should be converted to module-Levle constants (module-level constants) in Python. The advantage of this is that you do not need to enter the troublesome Foo. foo. somemethod is foo. somemethod, calling a class method will bring extra memory overhead. (Note: I personally think that in a non-pure Oo (Java pure Oo, C/C ++, python is not) language, sometimes functions can be used instead of classes, the function of the class is that all functions of the object can have shared data variables. If the object-level variables are not required, the namesapce mechanism is used for C ++, and use the Moudle mechanism in Python to use functions.) In addition, note that Foo. foo. in somemethod. there is a price, than Foo. somemethod is slow, because for Java. these names are searched by the compiler. For a dynamic language like python, they are searched at runtime, so it takes time to pay. 2. Do I need to use the swith statement? The practice in python is to use the dictionary dict (hash table) 3. XML is not the answer (note, I still don't know much about it, read the original article) 4. do not use getters or setters (note, Getter such as C ++ inline int getnum () const {return num}). The author thinks it is a waste of CPU time and time.
Program Staff time, the specific reason is not too understandable :) 5. Remove program redundancy. The author mentioned the use of closures to remove redundancy, but he did not give details
Code For example, how to remove it is not easy to understand :) about the closure Google, the following is an example
Take a look 《PythonCore programmingClosure.
If a variable in the external scope (but not in the global scope) is referenced in an internal function, the internal function is definedClosure. Variables defined in external functions that are referenced or used by internal functions are called free variables.
The following program running result
1
2
1
2
I personally think that closures can make the function have a non-Global (but class-global variable, nonlocal) feeling similar to the C ++ static variable. Its Class object can be completely simulated through the object variable, however, this is just functional programming.
1 Def Counter (start_at = 0 ):
2 Count = [Start_at]
3 Def Incr ():
4 Count [0] + = 1
5 Return Count [0]
6 Return Incr
7
8 Def Counter2 (S = 0 ):
9 Count = S
10 Def Incr ():
11 Nonlocal count
12 Count + = 1
13 Return Count
14 Return Incr
15
16 Count = Counter ()
17
18 Print (Count ())
19
20 Print (Count ())
21
22
23 Count = Counter2 ()
24 Print (Count ())
25
26 Print (Count ())