10 minutes a day, 12 days Getting Started Python (10)

Source: Internet
Author: User

# __str__ ()

Class Dog (object):

def __init__ (self,name):

Self.name = Name

def __str__ (self):

Return ' Dog object (%s) '% Self.name


Print Dog (' Papa ')

# <__main__. Dog Object at 0x1016b6cd0>

# Dog Object (Papa)


This is the result of the separate output before and after the __str__ () addition

# __iter__ ()


#!/usr/bin/env python

#-*-Coding:utf8-*-

__author__ = ' Shijn '


Class Fib (object):

def __init__ (self):

SELF.A,SELF.B = 0,1

def __iter__ (self):

return self

Def next (self):

SELF.A = self.b

self.b = SELF.A + self.b

If self.a>1000:

Raise Stopiteration ();

Return SELF.A


For n in Fib ():

Print n


# 1

# 2

# 4

# 8

# 16

# 32

# 64

# 128

# 256

# 512


# by __ITER__ () You can have a class that can be used by the for...in Loop, __iter__ () returns an iterative object, and then the Python Buddha, like a loop, constantly calls the next () method of the iteration object to get the next value of the loop, Know to exit the loop when encountering a stopiteration error.

# __getitem__ ()

# The above code is implemented so that a custom class can do a for...in loop like a list, but it doesn't get the corresponding data by subscript like a list, so fib () [2] doesn't work. You also need to implement a __getiterm__ () method inside the class to implement , according to its second parameter to determine the different situation can achieve subscript and slicing functions.


Class Fib (object):

def __getitem__ (self, item):

A, B =

For x in range (item):

A, B = b,a+b

Return a

f = Fib ()

Print F[20]

# 10946


# Similar to the __setitem__ () method, the object is treated as a list or dict to assign values to the set, and __delitem__ () is used to delete an element.

# __getattr__ ()

# If a property does not exist, an error will be made by default. If we want to control this place, or let some special cases not error, can pass the __getattr__ () method, for example, when calling a nonexistent property name, the Python interpreter tries to call __GETATTR__ ( Self, ' name ') to get the corresponding property.


Class Fib (object):

def __getattr__ (self, item):

If item = = ' Name ':

Return ' Sky '


f = Fib ()

Print F.name

# __call__ ()

# object instances often have their own properties and methods, and when we invoke instance methods, we use Instance.method () to invoke them. If you want to call directly on the instance itself, you only need to implement __call__ ().


Class Dog (object):

def __init__ (self,name):

Self.name = Name

def __call__ (self, *args, **kwargs):

print ' Dog name is%s '% Self.name


Dog = Dog (' Papa ')

Dog ()

# Dog Name is Papa


By using the callable (Dog ()) function, we can tell if an object can be called.

# type ()

# before it was introduced through type () gets the type of the object, and further it can also create a new type. For dynamic languages like Python, the definition of functions and classes is not only at compile time, but also at runtime.


def fun (self,name= ' Shijn '):

print ' Hello%s '% name


Hello = Type (' Hello ', (object,), Dict (hello=fun,age= ' 22 '))


h = Hello ()

H.hello ()

Print H.age

# Hello Shijn

# 22


# you can see that Hello is a complete class because the process of creating a class in Python is essentially scanning the syntax defined by class and then invoking type () to create the class. Three parameters are: class name, Inherited parent class (note the single tuple notation, can inherit more), The bound method or default property.

Metaclass

The concept of the meta-class is through its ability to create or modify classes, such as class, meta-class and so on, from this relationship to see the class can also be said to be an instance of the Meta class.

The use of meta-classes in general applications is very low frequency.

10 minutes a day, 12 days Getting Started Python (10)

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.