It's strange to see a function name in Python, __init__ I know the underlined function will run automatically, but I don't know the exact meaning of it.
See Today << Concise Python Tutorial >> 11th, Object-oriented programming, which describes it as: " annotations to c++/java/c# programmers
All of the class members (including data members) in Python are public and all methods are valid .
There is only one exception: if you use a data member name with a double-underscore prefix , for example __privatevar
, Python's name management system effectively uses it as a private variable.
This makes it a rule that if a variable is only meant to be used in a class or object, it should be prefixed with a single underscore. Other names will be public and can be used by other classes/objects. Remember that this is only a convention, not what Python requires (unlike a double underscore prefix).
Similarly, the attention __del__
method is similar to the concept of destructor . "
Suddenly the original __init__ in the class is used as a constructor, fixed also written, seemingly very rigid, in fact, there is a reason
def __init__ (self, name): "Initializes the person ' s data. " Self.name = name " print ' (Initializing%s) '% self.name # When the" is created, he/she # adds to the P Opulation person.population + = 1
name
A variable belongs to an object (it uses an self
assignment) and is therefore a variable of an object
self.name
Values are specified on a per-object basis, which indicates the nature of the variable that it acts as an object.
Remember, you can only use self
variables to refer to variables and methods of the same object. This is referred to as the attribute reference .
What exactly is __init__ in Python?