Questions about "Python is not Java"

I am a beginner programmer with basic Java experience and am currently learning Python. I came across this post in another question:

http://dirtsimple.org/2004/12/python-is-not-java.html

and I have a few questions regarding a published topic:


1) "Oh, and all these Foo.Bar.Baz attribute chains don’t come for free, ... so every point is counted."

Is the solution to this problem a problem of importing the module and its method in advance? For instance:

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 an if-then statments relationship.

There are several related answers on this topic, but they also raise a couple of questions:

  • Using if-else is cleaner, but it does not have the benefit of O (1) constant time in the switch statement.
  • Using a hash for constant time O (1)
  • Using a lambda function in a hash for comparison (not recommended)
    • Why is this not recommended? Is it because the lambda function removes a constant hash coefficient?
  • Using the bisect module
    • Does this method keep O (1) constant time, or is it another type of lambda function?
    • So, which method in Python is equal to the switch statement, with constant time O (1), and at the same time allowing the comparison operator?

3) Getters and setters are evil. Evil, evil ... do not write getters and setters ... This is what is built into the "property" ... In Python, this (getter and setter) is stupid, because you can start with a regular attribute and change your mind to any time without affecting class customers.

I do not quite understand this part.

In addition, it seems that in a public and private method or Python variable, you can easily access it, as opposed to open in C ++ and Java. Are there any constructive reasons for this behavior?


Finally, are there any recommendations for further good reading in Python compared to any other programming language?

+6
source share
3 answers
  • It rarely matters. If so, you'd better write C (or let Cython do it for you, with static type annotations so that it really counts) using PyPy (their JIT can completely remove such search queries and much more, even memory allocations, in specific loops ) etc.
  • lambda is in a different category than if-elif chain or bisect. What technique do you specifically mean? Many don't like lambda because they find it relatively detailed or unreadable (at least some of its uses can quickly become that way). bisect is just an ordered list, so the best search you get is a binary search O (log N). Do you need a switch statement? Use dicts. Comparison (other than == , of course) is beyond the scope of switch , and most things are usually compared with them and, in any case, makes O (1) lookup impossible.
  • How about this is not clear? When you write Python, you do not write getter or setter methods. You are using regular attributes. If you later see that you need additional logic when accessing / modifying an attribute, you turn it into a property and all the code using it can work without changes.
  • As for the lack of real confidentiality - they have asked and answered this many times, the short answer is that "we are all adults here," that is, programmers are trusted not to bother with private things unless they have a good reason. Another, weaker reason is that it is difficult to implement in a dynamic language, at least with the current object model (where, for example, methods are only normal functions).
+8
source
  • Often the answer is to use local names (variables) that refer to the specific instance of the object in question (for example, in the scope of your loop). The dynamic late-binding semantics of Python require its interpreter to go through everything ".". be referenced every time, because the objects to which each of them is attached can be modified by previous iterations through our loop. As with most other performance comments, it doesn't really matter until you work on a large scale (millions of repeating cycles multiplied by several layers of ".").

  • Sometimes the best approach is to rethink your design and use smart objects. Often, switch / case statements are used to implement different behaviors within the same object class, where a number of related classes can be implemented, each of which "does the right thing" based on its own type. In other cases, as follows from the commentary, you will create a dictionary ("hash") of keys and objects (functions, instances, lambdas, whatever) and use it as a send table.

  • Python allows you to access attributes directly (without getter / setter methods), and if you need to do this, you can use properties to ensure that your own code (getter / setter) is executed implicitly. The users of your code do not need to know or care that foo.bar=1 directly associates the new value with the foo bar attribute or whether it calls the foo method, which performs some international state manipulations that will be visible by accessing foo.bar later . These are implementation details. The presence of a language encourages programmers to inject all references to objects / attributes as explicit calls to β€œ.get ()” and β€œ.set ()”, while maintaining support for simpler ones. ”The syntax does not give an advantage to the code; it just makes the code more cluttered and harder to read.

Yes, attributes and methods can be easily accessed, and it’s difficult to β€œhide” them. This facilitates the use of the language. (The argument of the counter is that it makes it easier for classes to β€œabuse”, that is, it allows users to access the implementation details of the class that are not intended for public use ... this does not force them to respect the alleged abstractions. Then a moot point. If your class documents the alleged interface and implements them intelligently, then these are the interfaces that most programmers will use. If they feel the need to deceive your implementation details, you probably did it wrong --- and their alternative The goal is to simply re-do everything that you have done to circumvent the restrictions that you have imposed).

+5
source
  • May be. It is not always possible (or possible) to do specific imports in this way.

  • Lambda functions are disabled for the usual reasons, so lambda functions are disabled (not all of which I agree). bisect is a binary search, therefore O (log N).

  • Addiction Recovery

+1
source

Source: https://habr.com/ru/post/888246/


All Articles