I’m a novice programmer with basic Java experience, and currently learning Python.
I’ve stumbled across this blog post in another question thread:
http://dirtsimple.org/2004/12/python-is-not-java.html
and I’ve got a couple of questions regarding the topic posted:
1) “Oh, and all those Foo.Bar.Baz attribute chains don’t come for free, … , so each dot counts. “
Is the solution to this particular problem is importing module and its method beforehand? Such as:
from Foo.Bar import Baz
...
#now Baz() can be called directly without using Foo.Bar.Baz() everytime
2) Got a switch statement? The Python translation is a hash table, not a bunch of if-then statments.
There are several related answers regarding this topic, but they also raise a couple of questions:
- Use of if-else is cleaner, but it doesn’t have the advantage of constant time O(1) in switch statement.
- Use of hash for constant time O(1)
- Use of lambda function in hash for comparison (not recommended)
- Why is it not recommended? Is it because the lambda function removes the constant factor of hash?
- Use of bisect module
- Does this method retain the constant time O(1), or it is just another type of lambda function?
- So what method in Python, that is equal to switch statement, with constant time O(1), while at the same time allowing comparison statement?
3) Getters and setters are evil. Evil, evil…don’t write getters and setters … This is what the ‘property’ built-in is for … In Python, this (getter and setter) is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class.
I don’t really quite understand this part.
Also, it seems that in Python public and private method or variable can be easily accessed, in contrast of that in C++ and Java. Is there any design reason for this behavior?
Finally, is there any recommended further good read on Python vs. any other programming language?
lambdaas they consider it relatively verbose or unreadable (at least some of its uses can become this quickly).bisectis just maitaining an ordered list, so the best lookup you get is O(log N) binary search. You want a switch statement? Use dicts. Comparisions (apart from==, of course) are outside the scope ofswitchand most things commonly compared to them and make O(1) lookup impossible anyway.