Python 36th Day-----Special member method in class

Source: Internet
Author: User
Tags print object

__doc__ View the description of the tip

__MODULE__ represents the module where the object of the current operation is located

__CLASS__ represents the class to which the object of the current operation belongs

__init__ construction method automatically executes objects by class

__del__ destructor, the current object is freed in memory automatic chopper execution

The __call__ object is appended with parentheses to trigger execution

__dict__ viewing members in a class or object

__str__ If this method is defined in a class, output the return value of this method when printing such an object

__GETITEM__ when a property member of a dictionary is defined in a class, you can get

__setitem__ setting the data for a dictionary in a class

__DELITEM__ deleting data from a dictionary in a class

__metalass__ is used to indicate who created the class by whom it was instantiated.

__new__ triggering __init__ Creating an instance

1 #! Usr/bin/env python2 #-*-coding:utf-8-*-3 #Author Calmyan4 5  fromLib.ssImporta6 #Example Class Dog7 classDoges (object):8     """description information for the class"""9     def __init__(Self,name,food):TenSelf.name=name OneSelf.food= Food Aself.data={}#define a dictionary of a class -     def __call__(Self, *args, **kwargs):#execution of parentheses after the object -         Print(*args) the     def __str__(self):#default output return value -         returnSelf.name -     def __getitem__(self):#can get the dictionary of the class -         returnSelf.data +     def __setitem__(Self, key, value):#A dictionary of classes can be set -self.data[key]=value +     def __delitem__(Self, key):#You can delete the contents of a Dictionary of classes A         delSelf.data[key] atDog=doges ('XXX','III') - Print(dog.)__doc__) -b=A () - Print(b.__module__)#The module that operates the object - Print(dog.)__class__)#what is the class of the object currently being manipulated -Dog'111')# in Print(Doges.__dict__)#View a member of a class or object class only prints members of the class that do not print objects - Print(dog.)__dict__)#View members of a class or object only the members of the Print object do not print members of the class to Print(dog)#Print the return value of the __str__ + Print(dog.)__str__())#Print return value -  thedog['1']=1000#trigger. __setitem__ () *dog['2']=1000#trigger. __setitem__ () $ Print(dog.)__getitem__())Panax Notoginseng Print(dog.)__delitem__('1'))#Delete a dictionary in a class - Print(dog.)__getitem__()) the  + #special methods for setting classes A deffunc (self): the     Print('Hello word%s'%self.name) +     Print() -  $ def __init__(self,name,age): $Self.name=name -Self.age= Age - ##type参数 1: Class Name 2. class's base Class 3. Member of class, dictionary format theCat=type ('CAT', (object,), {'func': Func,'__init__':__init__}) - WuyiCat=cat ('Meow Meow', 3) the Cat.func () - Print(Cat.name,cat.age)
View Code

Reflection: Calling a method in an object through a string

1 #! Usr/bin/env python2 #-*-coding:utf-8-*-3 #Author Calmyan4 #Reflection5 6 #define a new method7 defBulk (self):8     Print("%s is talking ...."%self.name)9 Ten classDOG (object): One     def __init__(self,name): ASelf.name=name -     defEat (Self,food): -         Print('%s is eating%s'%(Self.name,food)) the  -D=dog ('a dog') -  -Stres=input ('Method:'). Strip ()#Manual Input Method +  - ifHasattr (d,stres):#reflects the method in the object through a string, whether there is +Func=getattr (D,stres)#Call this method AFunc"Bones")#Execution Method at Else: -SetAttr (D,stres,bulk)#object adds a method to return a memory address -GetAttr (D,stres) (d)#call this method to increase the -     #D.talk (d) - #Property Modification -Stres1=input ('Properties:'). Strip ()#Entering properties manually in ifHasattr (d,stres1):#if it already exists -Attr=getattr (D,STRES1)#called toA=input ('re-assign value:'). Strip () + setattr (d,stres1,a) -     Print(GetAttr (d,stres1)) the Else: *A=input ('newly assigned value:'). Strip () $SetAttr (D,stres1,a)#object adds a property that assigns the value of this propertyPanax Notoginseng     Print(stres1) -     Print(GetAttr (d,stres1)) the  + Print(d.__dict__) ADela=input ('To delete a property:'). Strip () the  + ifHasattr (D,dela):#if it already exists -Delattr (D,dela)#to delete $     Print('Delete succeeded') $  - Print(d.__dict__)
View Code

Python 36th Day-----Special member method in class

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.