python获取类的类属性_获取python中的类和实例的属性

1586010002-jmsa.png

In python work next code:

class MyClass(object):

field = 1

>>> MyClass.field

1

>>> MyClass().field

1

When I want return value for custom fields I use next code:

class MyClass(object):

def __getattr__(self, name):

if name.startswith('fake'):

return name

raise AttributeError("%r object has no attribute %r" %

(type(self).__name__, name))

>>> MyClass().fake

fake

But:

>>> MyClass.fake

Traceback (most recent call last):

File "", line 1, in

AttributeError: class MyClass has no attribute 'fake'

Ok, for classes I can use next code:

class MyClassMeta(type):

def __getattr__(cls, name):

if name.startswith('fake'):

return name

raise AttributeError("%r object has no attribute %r" %

(type(self).__name__, name))

class MyClass(object):

__metaclass__ = MyClassMeta

>>> MyClass.fake

fake

But:

>>> MyClass().fake

Traceback (most recent call last):

File "", line 1, in

AttributeError: 'MyClass' object has no attribute 'fake'

To resolve this problem I use next code:

class FakeAttrMixin():

def __getattr__(self, name):

if name.startswith('fake'):

return name

raise AttributeError("%r object has no attribute %r" %

(type(self).__name__, name))

class MyClassMeta(type, FakeAttrMixin):

pass

class MyClass(object, FakeAttrMixin):

__metaclass__ = MyClassMeta

>>> MyClass.fake

fake

>>> MyClass().fake

fake

MyClass.fake will call __getattr__ with MyClass and fake arguments.

MyClass().fake will call __getattr__ with MyClass instance and fake arguments.

And it's ok if I implement __getattr__ logic only on my mixin and don't use self argument.

Can I write custom value resolving by class and instance more beautiful and why field value resolving for MyClass.field and MyClass().field with MyClass(object): field = 1 definition works different if compare with __getattr__ method? Because when I want get field it at first searching in instance, then in class, but I can't understand why __getattr__ works another way.

解决方案

No, if you have to support both arbitrary attribute lookup on the class as well as the instance, then your only option is to implement a __getattr__ hook method on both the metaclass and the class, one each to support lookups on the class and the instance.

This is because special hook methods are always looked up on the type, so type(obj).__getattr__. Hence, for MyClass.fake the metaclass __getattr__ is used. See Special method lookup for new-style classes; I explained why this is in a previous answer.

The short reason is that in your case, MyClass.fake would translate into MyClass.__getattr__('fake') and __getattr__ is then an unbound method expecting two arguments (self and name), which would fail.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值