Python function in self class

Python class self in the end is Gansha

When writing class Python, each function argument first argument is self, beginning in the end I do not care it is doing, just know that you must write on. Later on Python became familiar with that concept of self and then look back, it seems a bit figured out.

First clear that self only in the class will have a method, independent of function or method it is not necessary with the self. When self class method definition must be some, although not necessarily the appropriate parameters passed when calling.

self name is not required, in python is not self keyword, you can be defined as a or b, or other names can be, but agreed to vulgar (and other programming languages ​​to unify and reduce the difficulty of understanding), not to engage in alternative, we will I do not understand.

The following example will self myname as to no error:

class Person:
    def _init_(myname,name): myname.name=name def sayhello(myname): print 'My name is:',myname.name p=Person('Bill') print p

refers to a self class instance object itself (note: not class itself).

class Person:
    def _init_(self,name): self.name=name def sayhello(self): print 'My name is:',self.name p=Person('Bill') print p

In the above example, self directed Personexamples p. Why do not point to the class itself, the following examples:

class Person:
    def _init_(self,name): self.name=name def sayhello(self): print 'My name is:',self.name p1=Person('Bill') p2 = Person('Apple') print p1

If the self points to the class itself, then when there are multiple instances of an object, self directed, which one do?

to sum up

self needs to be defined in the definition, but automatically when an incoming call.

self name is not specified dead, but it is better as agreed with self

self always refers to the instance of the class when calling.

Source: https://www.cnblogs.com/chownjy/p/8663024.html

Guess you like

Origin www.cnblogs.com/yibeimingyue/p/11225107.html