python中的特殊属性

特殊属性
查看属性

# 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']
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值