6.4 instructions
You can override method attributes with the same name for data attributes. This is to avoid unexpected name conflicts in large systems. Therefore, it is very effective to use some common methods to reduce conflicts. Common methods include: capital letter method name, using a unique string as the name of the Data Attribute (which can be an underscore _), verb naming, and name the data attribute.
Data attributes can be referenced by methods just like common users of objects. In other words, classes cannot be used to implement pure data types. In fact, in python, data cannot be forcibly hidden, and everything is based on conventions. (For example, written in C, the python implementation can completely hide the implementation details and, if necessary, control the access to objects, which can be extended through the C language)
Customers should exercise caution when using data attributes, and customers may confuse constants maintained by methods. And trample on their data attributes. Note that, as long as you can avoid duplication, you can add data attributes to the object instance without affecting the validity of the method-again, naming conventions can avoid a lot of trouble.
There is no convenient way to reference data attributes (or other methods) within a method. I found that this can increase the readability of the method. When browsing a method, it will not confuse local variables and instance variables easily.
Often, the first parameter of a method is self. In addition to the conventions, the name self has no special meaning for python. However, note that if you do not comply with this convention, your code may be poorly readable for other python programmers. It can also be understood that the class browser program may be written based on this Convention.
Any function object that acts as a class property defines a method for this class instance. In a class definition, function definitions are encapsulated and sometimes unnecessary. You can also assign a function object to a local variable in the class. For example:
# Function defined outside the class
Def f1 (self, x, y ):
Return min (x, x + y)
Class C:
F = f1
Def g (self ):
Return 'Hello world'
H = g
At present, f, g, and h all point to all attributes of function object class C, so they are all methods of class C instance -- C is actually equivalent to g. It should be noted that this habit will only confuse readers of the program.
You can use the method property self to call other methods.
Class Bag:
Def _ init _ (self ):
Self. data = []
Def add (self, x ):
Self. data. append (x)
Def addtwice (self, x ):
Self. add (x)
Self. add (x)
You can use a global name using the same method as a common function. And the corresponding global scope of the method is the module that contains the method definition. (A class can never be used as a global scope.) Although there are few good reasons to use global variables in methods, global scope has many legitimate uses. First, global-scope functions and modules can be used as methods, just as functions and classes defined in them. Generally, classes containing methods are defined in this global scope. In the next section, we will find a better reason to explain why a method can reference its own class.