Look at some problems of tutorial and solutions nine. Class 9.3.4 Method Object
class MyClass: "" " A Simple Example Class "" "= 12345 def f (self): return ' Hello world '= X.F while True: print XF ()
Reference: The special of the method is that the instance object is passed to the function as the first parameter of the function. In our example, calling X.f () is exactly the same as myclass.f (x). in general, calling a method with a list of n parameters is equivalent to inserting the object to which the method belongs before the first parameter of the list, and then invoking the corresponding function in the new list. If you still don't understand how the method works, it might help to understand its implementation. When you reference an instance property of a non-data property, its class is searched. If the name is recognized as a valid class property, which is a function object, the instance object and the function object are encapsulated in an abstract object: This is the method object. When a method object is called with a parameter list, it is re-unpacked, a new parameter list is constructed with the instance object and the original parameter list, and the corresponding function object is called with this new parameter list.
9.3.5 class and instance variables
In general, instance variables are used for data that is unique to each instance, and class variables are used for properties and methods shared by all instances of the class:
class MyClass: "" " A simple Example Class "" "= 12345 def f (self): return ' Hello World 'a= MyClass () b=MyClass () print A.I is B.iprint a.i is myclass.ia.i=5print a.i are b.iprint A.I is MyC Lass.i
Important WARNING: The default value is calculated only once. This makes the default value variable for objects such as lists, dictionaries, or instances of most classes to vary
class MyClass: "" " A Simple Example class "" "= 12345 def f (self): return ' Hello World ' def t (self,l=[]): l.append (' a ') return La= MyClass () b=MyClass () print a.t () print b.t ()
9.4 Additional Instructions
Typically, the first parameter of a method is called self. This is only a convention: the name Self has absolutely no special meaning for Python. Note, however, that if you do not follow this convention, your code will become less readable to other Python programmers, and some class viewer programs may be written in accordance with this Convention.
Problem:
class MyClass: "" " A Simple Example class "" "= 12345 def f (self): self.o=123 return ' Hello World ' def t (self,l=[]): l.append (' a ') return L def p (self): print self.o a=MyClass () A.P ()
Why is the P method not accessible to O, but if you define O in the __init__ function, you can access it.
9.8 Exceptions are also classes
class B: passclass C (b): passclass D (c): passfor c in [B, C, D]: Try : raise C () except D: "D" except C: "C" except B: "B"
9.9 iterators
classReverse:"" "Iterator for looping over a sequence backwards." "" "def __init__ (self, data): Self.data=Data Self.len=len (data) Self.index=-1def __iter__ (self):returnSelf def next (self):ifSelf.index = = Self.len-1: Self.index=-1Raise Stopiteration Self.index= Self.index + 1returnSelf.data[self.index]r=reverse (' abc ')) forx in R:print x print R.index9.11 Generator Expression
What is this?
Python Tutorial Note--9. Class