Detailed Pyhon methods, properties, iterators

Source: Internet
Author: User
This article details the methods, properties, iterators in Pyhon

Construction Method:

The constructor method represents an initialization method called Init, similar to the one used in the previous example.

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

>>> class FooBar:    def __init__ (self):        self.somevar=42        >>> f=foobar () >>> F.somevar>>> class Fosyntaxerror:invalid syntax>>> class FooBar ():    def __init__ (self,value=42) :        self.somevar=value        >>> f=foobar (' This was a constructor argument ') >>> F.somevar ' This is a Constructor argument '

Overriding general methods and special construction 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)  #调用超类的构造方法        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)

>>> __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!

Property

Attributes defined by accessors are called properties

>>> 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    >>> r=rectangle () >>> r.width=10>>> r.height=5 >>> r.getsize () (5) >>> r.setsize ((150,100)) >>> R.width

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 (5) >>> r.size=150,100>>> r.width

Iterators

The iterator is the one with 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>>> it=iter ([]) >>> It.next () >>> It.next () >>> 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]

Yield generator

Any function that contains a yield statement is called a generator. Each time a value is generated (using the yield statement), the function is frozen, that is, the function stops at that point and waits to be activated, and the function is activated and executed from the point of the stop.

>>> nested=[[1,2],[3,4],[5]]>>> def Flatten (nested): for Sublist in nested:for element in Sublist:yield element >>> for num in Flatten (nest ed): Print Num24 
Related Article

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.