Home >Backend Development >Python Tutorial >Introduction to special member methods in python classes
__doc__ View the description information of the tip
__module__ represents the module where the object of the current operation is located
__class__ represents the class of the object of the current operation
__init__ construction The method automatically executes the object created by the class
__del__ destructor method. The current object is released in the memory and is automatically executed.
Add parentheses after the __call__ object to trigger execution.
__dict__ View members in a class or object
__str__ If this method is defined in a class, then when printing such an object, the return value of this method is output
__getitem__ When the class is Defines the attribute members of a dictionary, which can be obtained
__setitem__Set and modify the data of the dictionary in the class
__delitem__Delete the data of the dictionary in the class
__metalass__It is used Indicates who instantiates and creates this class
__new__ triggers __init__ to create an instance
1 #!usr/bin/env python 2 #-*-coding:utf-8-*- 3 # Author calmyan 4 5 from lib.ss import a 6 #示例类 dog 7 class doges(object): 8 """类的描述信息""" 9 def __init__(self,name,food):10 self.name=name11 self.food=food12 self.data={}#定义一个类的字典13 def __call__(self, *args, **kwargs):#对象后面加括号解执行14 print(*args)15 def __str__(self):#默认输出返回值16 return self.name17 def __getitem__(self):#可以获取类的的字典18 return self.data19 def __setitem__(self, key, value):#可以设置类的的字典20 self.data[key]=value21 def __delitem__(self, key):#可以删除类的字典的内容22 del self.data[key]23 dog=doges('xxx','iii')24 print(dog.__doc__)25 b=a()26 print(b.__module__)#操作的对象的那个模块27 print(dog.__class__) #当前操作的对象的类是什么28 dog('111')#29 print(doges.__dict__)#查看类或对象的成员 类只打印类的成员不打印对象的成员30 print(dog.__dict__) #查看类或对象的成员 对象只打印对象的成员不打印类的成员31 print(dog)#打印 __str__的返回值32 print(dog.__str__())#打印返回值33 34 dog['1']=1000#触发.__setitem__()35 dog['2']=1000 #触发.__setitem__()36 print(dog.__getitem__())37 print(dog.__delitem__('1'))#删除类中字典38 print(dog.__getitem__())39 40 #设置类的特殊方法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参数 1:类名 2.类的基类 3.类的成员,字典格式49 CAT=type('CAT',(object,),{'func':func,'__init__':__init__})50 51 cat=CAT('喵喵',3)52 cat.func()53 print(cat.name,cat.age)View Code
1 #!usr/bin/env python 2 #-*-coding:utf-8-*- 3 # Author calmyan 4 #反射 5 6 #定义 一个新方法 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('一只狗')17 18 stres=input('方法:').strip()#手动输入方法19 20 if hasattr(d,stres):#通过字符串反射对象中的方法,是否存在21 func=getattr(d,stres)#调用此方法22 func("骨头")#执行方法23 else:24 setattr(d,stres,bulk)#对象增加一个方法 返回一个内存地址25 getattr(d,stres)(d)#调用此方法 增加的26 #d.talk(d)27 #属性修改28 stres1=input('属性:').strip()#手动输入属性29 if hasattr(d,stres1):#如果已经存在30 attr=getattr(d,stres1)#调用31 a=input('重新赋值:').strip()32 setattr(d,stres1,a)33 print(getattr(d,stres1))34 else:35 a=input('新赋值:').strip()36 setattr(d,stres1,a)#对象增加一个属性 ,赋值 返回这个属性的值37 print(stres1)38 print(getattr(d,stres1))39 40 print(d.__dict__)41 dela=input('删除属性:').strip()42 43 if hasattr(d,dela):#如果已经存在44 delattr(d,dela)#进行删除45 print('删除成功')46 47 print(d.__dict__)View Code
The above is the detailed content of Introduction to special member methods in python classes. For more information, please follow other related articles on the PHP Chinese website!