Introduction to classes and types in python and introduction to python types
What is a class?
It can be regarded as a synonym of type or type. All objects belong to a class called a class instance.
For example, birds are "Birds" instances. This is a general (abstract) class with many sub-classes: the bird you see may belong to the sub-class "lark ". You can think of "Birds" as a collection of all birds, and "lark birds" is a subset of them. When an object belongs to a subset of another object, the former is called a subclass of the latter. Therefore, "lark bird" is a subclass of "bird, "Birds" is a super category of "lark birds"
Defining subclasses is just a process of defining more methods.
Create class
>>> class Person: def setName(self,name): self.name=name def getName(self): return self.name def greet(self): print "Hello,world! I'm %s" % self.name >>> foo=Person()>>> bar=Person()>>> foo.setName('Nsds')>>> bar.setName('Ysdy')>>> foo.greet()Hello,world! I'm Nsds>>> bar.greet()Hello,world! I'm Ysdy
When the setName and greet functions of foo are called, foo automatically passes itself as the first parameter into the function, so it is named self. Without self, the member method will not be able to access the object they want to operate on its features.
Features are externally accessible:
>>> foo.name'Nsds'>>> bar.name='Yoda'>>> bar.greet()Hello,world! I'm Yoda
Features, functions, and methods
The self parameter is actually the difference between methods and functions. Method to bind their first parameter to the instance, so this parameter is not required. Therefore, you can bind a feature to a common function so that no special self parameter exists:
(A feature is a variable inside an object. The state of an object is described by its features. The object method can change its features and access the features directly from outside the object)
>>> class Class: def method(self): print 'I have a self!' >>> def function(): print "I don't...">>> s=Class()>>> s.method()I have a self!>>> s.method=function>>> s.method()I don't...
The variable birdsong references the binding method bird. sing and still accesses the self parameter (still bound to the same instance of the class)
>>> class Bird: song='Squaawk' def sing(self): print self.song >>> bird=Bird()>>> bird.sing()Squaawk>>> birdsong=bird.sing>>> birdsong()Squaawk
Adding a Double underline before the name can make the method or feature private (inaccessible from outside)
>>> class Secretive: def __inaccessible(self): print "Bet you can't see me..." def accessible(self): print "The secret message is:" self.__inaccessible() >>> s=Secretive()>>> s.__inacessible()Traceback (most recent call last): File "<pyshell#182>", line 1, in <module> s.__inacessible()AttributeError: 'Secretive' object has no attribute '__inacessible'>>> s.accessible()The secret message is:Bet you can't see me...
In the internal definition of a class, all the names opened with double underscores are "translated" into the form of a single underline and a class name.
>>> Secretive._Secretive__inaccessible<unbound method Secretive.__inaccessible>>>> s._Secretive__inaccessible()Bet you can't see me...
Class namespace
When defining a class, all the code in the class statement is executed in a special namespace-The namespace of the class. This namespace can be accessed by all members in the class.
Class definition is actually the Execution code block
>>> class MemberCounter: members=0 def init(self): MemberCounter.members+=1 >>> m1=MemberCounter()>>> m1.init()
>>> m1.members1>>> m1.members=2>>> m1.members2>>> m2=MemberCounter()>>> m2.init()>>> m2.members2>>> m2.init()>>> m2.members3>>> m1.members2>>>
The new members value is written to the m1 feature, shielding variables within the class range.
Superclass
>>> class Filter: def init(self): self.blocked=[] def filter(self,sequence): return [x for x in sequence if x not in self.blocked] >>> class SPAMFilter(Filter): def init(self): self.blocked=['SPAM'] >>> f=Filter()>>> f.init()>>> f.filter([1,2,3])[1, 2, 3]>>> s=SPAMFilter()>>> s.init()>>> s.filter(['SPAM','SPAM','egg','name','ff'])['egg', 'name', 'ff']
Inheritance, superclass
>>> class Filter: def init(self): self.blockes=[] def filter(self,sequence): return [x for x in sequence if x not in self.blocked] >>> class S(Filter): def init(self): self.blocked=['s'] >>> f=Filter()>>> f.init()>>> f.filter([1,2,3])
Multiple superclasses
The methods in the inherited class are overwritten and then the methods in the inherited class are
>>> class C(): def calculate(self,expression): self.value=eval(expression) >>> class Talker(): def talk(self): print 'Hi,my value is',self.value >>> class TalkingCalculator(C,Talker): pass>>> tc=TalkingCalculator()>>> tc.calculate('1+2*3')>>> tc.talk()Hi,my value is 7