首页 > 编程语言 > 详细

guxh的python笔记:特殊方法

时间:2019-01-13 17:39:47      阅读:148      评论:0      收藏:0      [点我收藏+]

 

 

 

class Foo:
    """this is Foo"""
    typecode = ‘d‘

    def __init__(self, x):
        self.x = x

 

__doc__:类的描述信息

print(f.__doc__)  # this is Foo

 

__module_:对象所属modul,如果是当前执行文件的即__main__,如果是import的,可以查看所属的module

 

print(f.__module__)  # __main__
import pandas as pd
df = pd.DataFrame()
print(df.__module__)  # pandas.core.frame

 

__class__:对象所属类,等效于type

print(f.__class__)   # <class ‘__main__.Foo‘>
print(type(f))       # <class ‘__main__.Foo‘>

 

 __call__:让类的实例变为可调用

class Foo:

    def __call__(self, *args, **kwargs):
        return 1

print(Foo()())   # 1,第一次()是实例化,第二次()是调用

 

__dict__:

打印所有实例的属性(不包括类属性):

f = Foo(1)
print(f.__dict__)   # {‘x‘: 1}

打印所有类属性(不包括实例的属性):

for k, v in Foo.__dict__.items():
    print(k, v)

输出为:
__module__ : __main__
__doc__ : this is Foo
typecode : d
__init__ : <function Foo.__init__ at 0x000001AC45314620>
__dict__ : <attribute ‘__dict__‘ of ‘Foo‘ objects>
__weakref__ : <attribute ‘__weakref__‘ of ‘Foo‘ objects>

 

__getitem__、__setitem__、__delitem__:[]运算符,获取/设置/删除分量

 

 

__new__:待补充

 

__metaclass__:待补充

 

 

 __init__:略

 

__del__:略

 

 

__str__:略

 

__repr__:略

 

guxh的python笔记:特殊方法

原文:https://www.cnblogs.com/guxh/p/10263208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!