Finding a list of all double-underscore variables?

前端 未结 4 1007
名媛妹妹
名媛妹妹 2020-12-08 01:26

Related: What is the common header format of Python files?

Where can I find a list of all double-underscore variables/keywords that are commonly used in Pyth

相关标签:
4条回答
  • 2020-12-08 01:35

    The complete list used by Python is given in the Python Language Reference section 3, "Data model". Every other one is non-standard or used by third-party modules and is documented separately.

    0 讨论(0)
  • 2020-12-08 01:47

    If you want to see magic names whether documented or not, go to the Lib directory and run:

    egrep -oh '__[A-Za-z_][A-Za-z_0-9]*__' *.py | sort | uniq
    

    That produces:

    '__all__'
    '__args__'
    '__author__'
    '__bases__'
    '__builtin__'
    '__builtins__'
    '__cached__'
    '__call__'
    '__class__'
    '__copy__'
    '__credits__'
    '__date__'
    '__decimal_context__'
    '__deepcopy__'
    '__dict__'
    '__doc__'
    '__exception__'
    '__file__'
    '__flags__'
    '__ge__'
    '__getinitargs__'
    '__getstate__'
    '__gt__'
    '__import__'
    '__importer__'
    '__init__'
    '__ispkg__'
    '__iter__'
    '__le__'
    '__len__'
    '__loader__'
    '__lt__'
    '__main__'
    '__module__'
    '__mro__'
    '__name__'
    '__package__'
    '__path__'
    '__pkgdir__'
    '__return__'
    '__safe_for_unpickling__'
    '__setstate__'
    '__slots__'
    '__temp__'
    '__test__'
    '__version__'
    
    0 讨论(0)
  • 2020-12-08 01:48

    There is a comprehensive list of the dunders and their usage that I like here: magicmethods

    0 讨论(0)
  • 2020-12-08 01:50

    when i use

    dir(object)
    
    

    i got these:

    
    '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__has
    h__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__'
    , '__setattr__', '__sizeof__', '__str__', '__subclasshook__'
    

    and i think they are the dunder names every object will have in python

    0 讨论(0)
提交回复
热议问题