Home >Backend Development >Python Tutorial >Detailed explanation of methods, properties, and iterators in Python

Detailed explanation of methods, properties, and iterators in Python

高洛峰
高洛峰Original
2017-03-10 19:25:561426browse

This article explains in detail the methods, properties, and iterators in Python

Construction method:

The construction method represents an initialization method called init similar to the one used in previous examples.

When an object is created, the constructor will be called immediately

>>> class FooBar:
    def __init__(self):
        self.somevar=42

        
>>> f=FooBar()
>>> f.somevar
>>> class fO
SyntaxError: 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 constructors

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

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

Iterator

An iterator is one with 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([1,2,3])
>>> 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 will be frozen, that is, the function stops at that point waiting to be activated. After the function is activated, it will start executing from the point where it stopped

>>> nested=[[1,2],[3,4],[5]]
>>> def flatten(nested):
    for sublist in nested:
        for element in sublist:
            yield element

            
>>> for num in flatten(nested):
    print num
2
4


The above is the detailed content of Detailed explanation of methods, properties, and iterators in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn