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__来实例化的。

  • 相关阅读:
    Linux基本权限管理
    Spring JMS
    消息中间件 ActiveMQ的简单使用
    Ionic slides 轮播图
    Spring 3 MVC and XML example
    Java 数组
    Java String类
    Java static 使用
    http://blog.csdn.net/huang_xw/article/details/7090173
    http://blog.chinaunix.net/uid-20577907-id-3519578.html
  • 原文地址:https://www.cnblogs.com/endust/p/12326039.html
Copyright © 2011-2022 走看看