Python 36th days ----- special member methods in the class, python 36th days

Source: Internet
Author: User

Python 36th days ----- special member methods in the class, python 36th days

_ Doc _ view the description of the tip

_ Module _ indicates the module where the current operation object is located

_ Class _ indicates the class to which the object of the current operation belongs.

_ Init _ constructor creates an object through the class and runs it automatically.

_ Del _ destructor. The current object is released in the memory and executed automatically.

_ Call _ an object is followed by parentheses to trigger execution

_ Dict _ view members in a class or object

_ Str _ if this method is defined in a class, the return value of this method is output when such an object is printed.

_ Getitem _ when a dictionary attribute member is defined in the class, you can obtain

_ Setitem _ set to modify Dictionary data in the class

_ Delitem _ Delete Dictionary data in the class

_ Metalass _ indicates who will instantiate and create the class.

_ New _ trigger _ init _ create an instance

1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 5 from lib. ss import a 6 # example class dog 7 class doges (object): 8 "class description" 9 def _ init _ (self, name, food): 10 self. name = name11 self. food = food12 self. data ={}# define a class dictionary 13 def _ call _ (self, * args, ** kwargs ): #14 print (* args) 15 def _ str _ (self): # default output return value 16 return self. name17 def _ getitem _ (self): # You can obtain the dictionary of the Class 18 return self. data19 def _ setitem _ (self, key, value): # You can set the class's dictionary 20 self. data [key] = value21 def _ delitem _ (self, key): # You can delete the dictionary content of the Class 22 del self. data [key] 23 dog = doges ('xxx', 'II') 24 print (dog. _ doc _) 25 B = a () 26 print (B. _ module _) # module 27 print (dog. _ class _) # What is the class of the object in the current operation 28 dog ('000000') #29 print (doges. _ dict _) # view the member classes of a class or object, print only the members of the class, and do not print 30 print (dog. _ dict _) # view the Member objects of a class or object, print only the members of the object, and do not print 31 print (dog) members of the class) # print the return value of _ str _ 32 print (dog. _ str _ () # print the returned value 33 34 dog ['1'] = 1000 # Trigger. _ setitem _ () 35 dog ['2'] = 1000 # Trigger. _ setitem _ () 36 print (dog. _ getitem _ () 37 print (dog. _ delitem _ ('1') # Delete the 38 print (dog. _ getitem _ () 39 40 # special method for setting classes 41 def func (self): 42 print ('Hello word % s' % self. name) 43 print () 44 45 def _ init _ (self, name, age): 46 self. name = name47 self. age = age48 # type parameter 1: class name 2. class base class 3. class member, Dictionary format 49 CAT = type ('cat', (object,), {'func': func, '_ init _' :__ init __}) 50 51 cat = CAT ('miaomiao ', 3) 52 cat. func () 53 print (cat. name, cat. age)
View Code

Reflection: Call methods in an object using a string

1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 # reflection 5 6 # define a new method 7 def bulk (self ): 8 print ("% s is talking .... "% self. name) 9 10 class DOG (object): 11 def _ init _ (self, name): 12 self. name = name13 def eat (self, food): 14 print ('% s is eating % s' % (self. name, food) 15 16 d = DOG ('one dog') 17 18 stres = input ('method :'). strip () # manually input method 19 20 if hasattr (d, stres): # Whether 21 func = getattr (d, stres) exists by using the method in the string reflection object) # Call this method 22 func ("bone") # Run Method 23 else: 24 setattr (d, stres, bulk) # Add a method to the object and return a memory address 25 getattr (d, stres) (d) #26 # d added to call this method. talk (d) 27 # Attribute Modification 28 stres1 = input ('attribute :'). strip () # manually enter the attribute 29 if hasattr (d, stres1): # if 30 attr = getattr (d, stres1) already exists # Call 31 a = input ('re-assigned value: '). strip () 32 setattr (d, stres1, a) 33 print (getattr (d, stres1) 34 else: 35 a = input ('new value :'). strip () 36 setattr (d, stres1, a) # Add an attribute to the object, and assign a value to return the value of this attribute 37 print (stres1) 38 print (getattr (d, stres1 )) 39 40 print (d. _ dict _) 41 dela = input ('delete attribute :'). strip () 42 43 if hasattr (d, dela): # if 44 delattr (d, dela) already exists # Delete 45 print ('deleted successfully ') 46 47 print (d. _ dict __)
View Code

 

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.