Method, attribute, iterator, and pyhon Attribute Generator in pyhon

Source: Internet
Author: User

Method, attribute, iterator, and pyhon Attribute Generator in pyhon

Constructor:

The constructor is similar to the initialization method named init used in the previous example.

When an object is created, the constructor is called immediately.

>>> class FooBar:    def __init__(self):        self.somevar=42        >>> f=FooBar()>>> f.somevar42>>> class fOSyntaxError: invalid syntax>>> class FooBar():    def __init__(self,value=42):        self.somevar=value        >>> f=FooBar('This is a constructor argument')>>> f.somevar'This is a constructor argument'

Override general methods and special constructor Methods

>>> Class Bird: def _ init _ (self): self. hungry = True def eat (self): if self. hungry: print 'aaaah... 'self. hungry = False else: print 'no, thanks! '>>> B = Bird () >>> B. eat () Aaaah... >>> B. eat () No, thanks! >>> Class SongBird (Bird): def _ init _ (self): Bird. _ init _ (self) # Call the superclass constructor self. sound = 'squawk! 'Def sing (self): print self. sound >>> sb = SongBird () >>> sb. sing () Squawk! >>> Sb. eat () Aaaah...> sb. eat () No, thanks!

Super Function

Super (SongBird, self)

For more information, see: http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035005.html

>>> __metaclass__=type>>> class Bird:    def __init__(self):        self.hungry=True    def eat(self):        if self.hungry:            print 'Aaaah...'            self.hungry=False        else:            print 'No,thinks!'            >>> class SongBird(Bird):    def __init__(self):        super(SongBird,self).__init__()        self.sound='Squawk!'    def sing(self):        print self.sound        >>> n=SongBird()>>> n.sing()Squawk!>>> n.eat()Aaaah...>>> n.eat()No,thinks!

Attribute

The features defined by the accessors are called attributes.

>>> Class Rectangle: def _ init _ (self): self. width = 0 # feature self. height = 0 # feature def setSize (self, size): # Use the accessor method to change the feature self. width, self. height = size def getSize (self): # return self. width, self. height >>> r = Rectangle () >>> r. width = 10 >>> r. height = 5 >>> r. getSize () (10, 5) >>> r. setSize (150,100) >>> r. width150

Property Function

>>> __metaclass__=type>>> class Rectangle:    def __init__(self):        self.width=0        self.height=0    def setSize(self,size):        self.width,self.height=size    def getSize(self):        return self.width,self.height    size=property(getSize,setSize)    >>> r=Rectangle()>>> r.width=10>>> r.height=5>>> r.size(10, 5)>>> r.size=150,100>>> r.width150

Iterator

The iterator has the next

 

>>> class Fibs:    def __init__(self):        self.a=0        self.b=1    def next(self):        self.a,self.b=self.b,self.a+self.b        return self.a    def __iter__(self):        return self    >>> fibs=Fibs()>>> for f in fibs:    if f>1000:        print f        break    1597>>> it=iter([1,2,3])>>> it.next()1>>> it.next()2>>> class TestIterator:    value=0    def next(self):        self.value+=1        if self.value>10: raise StopIteration        return self.value    def __iter__(self):        return self    >>> ti=TestIterator()>>> list(ti)[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.