版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)
# animal.py
class Animal:
x = 123
def __init__(self, name):
self.name = name
self.age = 20
self.weight = 20
y = 200
print('animal Module\'s names = {}'.format(dir())) # 模块的属性
# animal Module's names = ['Animal', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__',
# '__loader__', '__name__', '__package__', '__spec__', 'y']
# cat.py
import animal
from animal import Animal
class Cat(Animal):
x = 'cat'
y = 'abcd'
class Dog(Animal):
def __dir__(self): # 返回可迭代对象的返回值
return ['dog'] # 必须返回可迭代对象,其内容必须是字符串
print('Current Module\'s names = {}'.format(dir()))
print('Current Module\'s names = {}'.format(dir(animal)))
# animal Module's names = ['Animal', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
# '__name__', '__package__', '__spec__']
# Current Module's names = ['Animal', 'Cat', 'Dog', '__annotations__', '__builtins__', '__cached__', '__doc__',
# '__file__', '__loader__', '__name__', '__package__', '__spec__', 'animal']
print("object's __dict__ = {}".format(sorted(object.__dict__.keys())))
# object's __dict__ = ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
# '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__',
# '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
print("Animal's dir() = {}".format(dir(Animal))) # 类Animal的dir()
# Animal's dir() = ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
# '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__',
# '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
# '__subclasshook__', '__weakref__', 'x']
print("Cat's dir() = {}".format(dir(Cat))) # 类Cat的dir()
# Cat's dir() = ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
# '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
# '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
# '__str__', '__subclasshook__', '__weakref__', 'x', 'y']
print('~~~~~~~~~~~~~~~~')
tom = Cat('tom')
print(sorted(dir(tom))) # 实例tom的属性、Cat类及所有祖先类的类属性
print(sorted(tom.__dir__())) # 同上
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
# '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
# '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
# 'age', 'name', 'weight', 'x', 'y']
print("Dog's dir = {}".format(dir(Dog))) # Animal类中的x也在其中
dog = Dog('snoppy')
print(dir(dog)) # ['dog']
print(dog.__dict__) # {'name': 'snoppy', 'age': 20, 'weight': 20}
内建函数
1. locals()返回当前作用域中的变量字典
2. globals当前模块全局变量的字典
class Person:
def show(self):
a = 100
t = int(a)
print(1, dir()) # 结果为列表 1 ['a', 'self', 't']
print(2, locals()) # 结果为字典 2 {'t': 100, 'a': 100, 'self': <__main__.Person object at 0x0000000001E496A0>}
print(3, locals().keys())
def test(a=50, b=100):
c = 180
print(4, dir()) # 4 ['a', 'b', 'c']
print(5, locals()) # 5 {'c': 180, 'b': 100, 'a': 50}
Person().show()
test()
print(6, dir()) # 6,7,8三者的结果一样
print(7, sorted(locals().keys())) # globals和locals返回时字典
print(8, sorted(globals().keys()))
# 8 ['Animal', 'Person', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
# '__name__', '__package__', '__spec__', 'test', 'y']