码迷,mamicode.com
首页 > 其他好文 > 详细

关于pyhton中的__xxx__格式的方法与变量的理解

时间:2018-04-16 16:11:36      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:col   signature   tco   查看   getattr   abs   理解   lse   定义   

python中类似__xx__的方法和变量是python系统内定义的方法和变量,都是具有特殊意义的基础变量和方法,一般不要擅自使用,除非知道自己在干什么。

具体查看python内置模块builtins.py,里面定义了内置方法、对象和异常,这些定义是最基本的定义。

一、__xx__格式的方法:

一般定义在object类中,object类是大多数python类的基类,这些__xx__格式的方法代表了内置方法,是允许一个类的重要属性。很多方法会被自动调用:

如类初始化时调用__init__()方法,str(obj)调用__str__()方法

内置函数与__xx__格式函数对应表:

dir  __dir__

str __str__

dict __dict__

等等

class object:
""" The most base type """
def __delattr__(self, *args, **kwargs): # real signature unknown
""" Implement delattr(self, name). """
pass

def __dir__(self): # real signature unknown; restored from __doc__
"""
__dir__() -> list
default dir() implementation
"""
return []

def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass

def __format__(self, *args, **kwargs): # real signature unknown
""" default object formatter """
pass

def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass

def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass

def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass

def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass

def __init_subclass__(self, *args, **kwargs): # real signature unknown
"""
This method is called when a class is subclassed.

The default implementation does nothing. It may be
overridden to extend subclasses.
"""
pass

def __init__(self): # known special case of object.__init__
""" Initialize self. See help(type(self)) for accurate signature. """
pass

def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass

def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass

@staticmethod # known case of __new__
def __new__(cls, *more): # known special case of object.__new__
""" Create and return a new object. See help(type) for accurate signature. """
pass

def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass

def __reduce_ex__(self, *args, **kwargs): # real signature unknown
""" helper for pickle """
pass

def __reduce__(self, *args, **kwargs): # real signature unknown
""" helper for pickle """
pass

def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass

def __setattr__(self, *args, **kwargs): # real signature unknown
""" Implement setattr(self, name, value). """
pass

def __sizeof__(self): # real signature unknown; restored from __doc__
"""
__sizeof__() -> int
size of object in memory, in bytes
"""
return 0

def __str__(self, *args, **kwargs): # real signature unknown
""" Return str(self). """
pass

@classmethod # known case
def __subclasshook__(cls, subclass): # known special case of object.__subclasshook__
"""
Abstract classes can override this to customize issubclass().

This is invoked early on by abc.ABCMeta.__subclasscheck__().
It should return True, False or NotImplemented. If it returns
NotImplemented, the normal algorithm is used. Otherwise, it
overrides the normal algorithm (and the outcome is cached).
"""
pass

__class__ = None # (!) forward: type, real value is ‘‘ __dict__ = {} __doc__ = ‘‘ __module__ = ‘‘

二、__xx__格式的变量:

 

关于pyhton中的__xxx__格式的方法与变量的理解

标签:col   signature   tco   查看   getattr   abs   理解   lse   定义   

原文地址:https://www.cnblogs.com/zhuluqing/p/8856007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!