Python-内置类属性
时间:2020-02-23 14:42:28 来源:igfitidea点击:
在本教程中,我们将学习Python中的内置类属性。
内置的类属性为我们提供了有关类的信息。
我们可以使用.
运算符来访问内置的类属性。
以下是内置的类属性。
属性 | 描述 |
---|---|
__dict__ | 这是存放类名称空间的字典。 |
__doc__ | 如果存在文档,这将为我们提供类文档。否则为"无"。 |
__name__ | 这为我们提供了类名。 |
__module__ | 这给我们定义了该类的模块的名称。在交互模式下,它会给我们__main__ 。 |
__bases__ | 一个可能为空的元组,包含按出现顺序排列的基类。 |
__doc__
类属性
在下面的Python程序中,我们将使用文档创建Awesome
类。
# class class Awesome: 'This is a sample class called Awesome.' def __init__(self): print("Hello from __init__ method.") # class built-in attribute print(Awesome.__doc__)
上面的代码将为我们提供以下输出。
This is a sample class called Awesome.
__name__
类属性
在下面的示例中,我们正在打印类的名称。
# class class Awesome: 'This is a sample class called Awesome.' def __init__(self): print("Hello from __init__ method.") # class built-in attribute print(Awesome.__name__)
Awesome
__module__
类属性
在下面的示例中,我们将打印该类的模块。
# class class Awesome: def __init__(self): print("Hello from __init__ method.") # class built-in attribute print(Awesome.__module__)
__main__
__bases__
类属性
在下面的示例中,我们将打印该类的基础。
# class class Awesome: def __init__(self): print("Hello from __init__ method.") # class built-in attribute print(Awesome.__bases__)
(<class 'object'>,)
__dict__
类属性
在下面的示例中,我们将打印类的字典。
# class class Awesome: def __init__(self): print("Hello from __init__ method.") # class built-in attribute print(Awesome.__dict__)
{'__module__': '__main__', '__doc__': 'Awesome class sample documentation.', '__init__': <function Awesome.__init__ at 0x106e2c1e0>, '__dict__': <attribute '__dict__' of 'Awesome' objects>, '__weakref__': <attribute '__weakref__' of 'Awesome' objects>}