Python Learning (ix) Magic methods, properties, and iterators

Source: Internet
Author: User

9.1 Preparatory work

All classes of Python 3.0 implicitly become subclasses of object.

9.2 Construction Methods

Create a constructor in Python: Just change the name of the Init method to the Magic version __init__.

class FooBar:     def __init__ (self):         =      >>> f = FooBar ()>>> F.somevar42

 __DEF__: Is a destructor method, but the call time is uncertain, so it is not recommended.

  9.2.1 overriding general methods and special construction methods

  A subclass method can override a method of a parent class.

After the constructor is overwritten, if the constructor of the parent class is not called, the properties of some of the parent classes may not be initialized.

The following two summaries are the two solutions that call a superclass construction method.

9.2.2 Call Unbound Superclass construction method

  

class SongBird (Bird):     def __init__ (self):         //Call the constructor of a super class directly        Bird. __init__ (self)          XXXX

9.2.3 using the Super function

  

class SongBird (Bird):     def __init__ (self):       Super (Songbrid, self).  __init__  ()        'squawk! '    def Sing (self):        XXX

9.3 Member access

  9.3.1 Basic sequence and mapping rules

__len__ (self)

__getitem__ (Self.key)

__SETITEM__ (Self.key, value)

__delitem__ (Self.key)

9.3.2 subclass lists, dictionaries, and strings

9.4 More Magic

9.5 Properties

  9.5.1 Property function

  Size = Property (GetSize (), SetSize ())

9.5.2 static methods and class member methods

9.5.3 __getattr__, __setattr__ and his friends

  It is possible to intercept all properties of an object when accessed.

__GETATTRBIUTE__:

__getattr_ (self, name)

__setattr__ (self, name, value)

__delattr__ (self, name)

9.6 iterators

__iter__:

  9.6.1 iterator Rules

9.6.2 getting sequences from iterators

9.7 Generators

 9.7.1 Creating a generator

9.7.2 Recursive generator

9.7.3 Universal Generator

9.7.4 Generator method

9.7.5 Simulation Generator

9.8 Eight Queen's question

  9.8.1 Generator and backtracking

9.8.2 problems

9.8.3 Status Indication

9.8.4 Looking for Conflict

9.8.5 Basic situation

9.8.6 need a recursive condition

9.8.7 Packaging

  

Python Learning (ix) Magic methods, properties, and iterators

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.