zoukankan      html  css  js  c++  java
  • Python类的特殊成员方法

    __doc__

    类的描述信息。

    class dog:
    	'''这是狗的类'''
    	def __init__(self,name):
    		self.name = name
    		self.__age = None
    
    
    print(dog.__doc__)
    #输出
    这是狗的类
    

      

    __module__

    表示当前操纵的对象在哪个模块。

    __class__

    表示当前操作的对象的类是什么。

    __call__

    class dog:
    	'''这是狗的类'''
    	def __init__(self,name):
    		self.name = name
    		self.__age = None
    
    	def __call__(self, *args, **kwargs):
    		print(*args, **kwargs)
    
    
    d = dog("小花")
    d("小花","小黑","布丁")
    

      

    __dict__

    打印所有属性

    class dog:
    	'''这是狗的类'''
    	def __init__(self,name):
    		self.name = name
    		self.__age = None
    
    d = dog("小花")
    print(dog.__dict__)#打印类里的所有属性,不包括实例属性
    print(d.__dict__)#打印所有实例属性,不包括类属性
    
    #输出
    {'__module__': '__main__', '__doc__': '这是狗的类', '__init__': <function dog.__init__ at 0x0000024E354F21E0>, '__dict__': <attribute '__dict__' of 'dog' objects>, '__weakref__': <attribute '__weakref__' of 'dog' objects>}
    {'name': '小花', '_dog__age': None}
    

      

      

    __str__

    class dog:
    	'''这是狗的类'''
    	def __init__(self,name):
    		self.name = name
    		self.__age = None
    
    	def __str__(self):
    		return "<obj:%s>" % self.name
    
    d = dog("小花")
    print(d)
    
    #输出
    <obj:小花>
    

      

    __new__

    先触发__new__再触发__init__。实质上是__new__来实例化的。

  • 相关阅读:
    bzoj 1821: [JSOI2010]Group 部落划分 Group
    codevs 1217 借教室
    洛谷 P2678 跳石头
    洛谷 P1316 丢瓶盖
    洛谷 P2683 小岛
    洛谷 P2431 正妹吃月饼
    loj #6092. 「Codeforces Round #418」恋爱循环
    loj #6091. 「Codeforces Round #418」幻想特快
    loj #6090. 「Codeforces Round #418」尘封思绪
    前端移植说明
  • 原文地址:https://www.cnblogs.com/endust/p/12326039.html
Copyright © 2011-2022 走看看