Several variables in the 1.python
1) __x If you want the internal properties to be inaccessible externally, you can add the name of the property with two underscores __ , and in Python, the variable name of the instance __ becomes a private variable (private), which can only be accessed internally and cannot be accessed by the external
is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. cannot be accessed directly __name because the Python interpreter __name has changed the variable _Student__name so that it can still _Student__name access the __name variable
2) __x__ Note that in Python, the variable name is similar, that is, starting with a double underscore, and ending with a double underscore, __xxx__ is a special variable, the special variable is directly accessible, not the private variable, so, can not be used __name__ , __score__ Such a variable name.
3) _x There are times when you see an instance variable name that starts with an underscore, such as an _name instance variable that can be accessed externally, but, as you see in the rules, when you look at such a variable, it means, "although I can be accessed, but, please treat me as a private variable, do not arbitrarily access ”。
Several special functions in 2.python
__func says it does not want external access, but it can actually be accessed, which embodies the design philosophy of Python, providing only advice, all by self-awareness.
__func__ are functions that can be overridden, implicitly called by other functions, which are actually methods for customizing classes
__len__ Len () actually called the __len__
__STR__ is implicitly called by print
__REPR__ is called directly when the variable is displayed (the difference between the two is to return the string that the __str__() user sees, and the __repr__() string that the program developer sees, that __repr__() is, for debugging services)
__iter__ if a class wants to be used for for ... in loops, like a list or a tuple, it must implement a __iter__() method that returns an iterative object, and then the python for loop constantly calls the iteration object's next() method to get the next value of the loop To exit the loop until a stopiteration error is encountered. (It is also often necessary to override the next method)
__getitem__ to act like a list to take out elements according to the subscript, you need to implement the __getitem__() method
_setitem__()method, the object is treated as a list or dict to assign values to the collection. Finally, there is a __delitem__() way to delete an element
__slot__ used to restrict the properties that a class can add
Python allows you to define a special variable when defining a class __slots__ to limit the attributes that the class can add. It __slots__ is important to note that the __slots__ defined properties work only on the current class and do not work on inherited subclasses. Unless it is also defined in a subclass __slots__ , the subclass allows the defined property to be its own __slots__ plus the parent class __slots__ .
__getattr__ Normally, when we call a method or property of a class, it will be an error if it does not exist. To avoid this error, there is another mechanism for Python to write a __getattr__() method that dynamically returns an attribute, except that it can add properties that are not originally available. Only if the attribute is not found, the existing attribute is called, __getattr__ for example, it is name not __getattr__ searched in.
__call__ you only need to define a __call__() method, you can invoke the instance directly, and __call__() you can define the parameters. Making a direct call to an instance is like calling a function, so you can think of the object as a function and the function as an object because there is no fundamental difference between the two. How do you tell if a variable is an object or a function? In fact, more often, we need to determine whether an object can be called, the object that can be called is an Callable object, such as the function and the __call()__ class instance we defined above
1) type () to determine object types
2) isinstance () Determine the object type and decide whether to inherit a parent class
3) dir () If you want to get all the properties and methods of an object, you can use a dir() function that returns a list containing a string
4) Help () get the details of the object
Simply listing properties and methods is not enough, mates getattr() , setattr() and hasattr() , you can also manipulate the state of an object directly, which is similar to reflection in other high-level languages
5) Python's built-in @property decorator is responsible for turning a method into a property call, which is used for getter, which corresponds to the @ attribute. Setter
Python Learning note two