In my previous post, I disussesed with the reader the concept of metaclass and its internals. this post is inspired from this post from stackoverflow.
In this post, I will visit one of the use of meta class so so that we can control the access to the class/static variable through the use of __getattr__ call
class GetAttrOnStaticClassVariableMetaclass(type):
'''
This is a use case that uses the Metaclass to create a classes that you can access the Class/Static variable
via call to __getattr__
'''
# due to some stupid reaon, I could not use cls as the first parameter, I have to use the
# self as the first parameter, which is really dume
def _foo_func(self):
return 'foo!'
def _bar_func(self):
return 'bar!'
def __getattr__(self, key):
if (key == 'Foo'):
return self._foo_func()
elif (key == 'Bar'):
return self._bar_func()
raise AttributeError(key)
def __str__(self):
return 'custom str for %s' % (self.__name__,)
and the following is the unit test case to show how to use it .
import unittest
from MetaClass.GetAttrOnStaticClassVariableMetaclass import GetAttrOnStaticClassVariableMetaclass
class Test(unittest.TestCase):
def test_getAttrOnStaticClassVariable_should_access_static_variables(self):
class Foo:
__metaclass__ = GetAttrOnStaticClassVariableMetaclass
self.assertEqual(True, hasattr(Foo, "Foo"), "Unable to get Static Class variable")
self.assertEqual(True, hasattr(Foo, "Bar"), "Unable to get Static Class variable")
self.assertEqual('foo!', Foo.Foo, "Unable to get Static Class variable")
self.assertEqual('bar!', Foo.Bar, "Unable to get Static Class variable")
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testGetAttrOnStaticClassVariable_should_access_static_variables']
unittest.main()
分享到:
相关推荐
__get__、__getattr__、__getattribute都是访问属性的方法,但作用不太相同,这里我们就来详解Python中 __get__和__getattr__和__getattribute__的区别:
主要介绍了对比Python中__getattr__和 __getattribute__获取属性的用法,注意二者间的区别,__getattr__只作用于不存在的属性,需要的朋友可以参考下
主要介绍了Python __setattr__、 __getattr__、 __delattr__、__call__用法示例,本文分别对这几个魔法方法做了讲解,需要的朋友可以参考下
魔法函数 getattr 与 getattribute 简单说明 __getattr__ 与 __getattribute__ 魔法函数的使用 目录魔法函数 __getattr__ 与 __getattribute____getattr__魔法函数作用__getattribute__魔法函数总结 书上说,天下...
主要给大家介绍了关于python中__init__、__new__和__call__方法的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友可以参考学习,下面来跟着小编一起看看吧。
class Book(object): def __setattr__(self, name, value): if name == ‘value’: object.__setattr__(self, name, value – 100) else: object.__setattr__(self, name, value) def __getattr__(self, name)...
下面小编就为大家带来一篇浅谈python中的getattr函数 hasattr函数。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
2.getattr函数:可以给一个默认值,以免触发错误。 writte=getattr(obj,'write',sys.stdout.write) 3.type函数:即可以得到一个对象的类型,也可以直接由它创建一个新类型: >>> Point=type('Point',(object,),{...
本文实例讲述了Python3.6简单反射操作。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #!python3 # ----------------------- # __Author : tyran # __Date : 17-11-13 # ----------------------- class...
看了a_byte_of_python 过后看看这个又会有新的提高了哦. Dive Into Python.................................................................................................................1 Chapter 1. 安装 ...
安装Scrapy出错,需要安装Microsoft Visual C++ 14.0 ERROR: Command errored out with exit status 1: 'd:\python3.8.1\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\z ...
文章目录面向对象高级编程使用__slots__使用__slots__使用@property练习多重继承Mixln小结定制类__str____iter____getitem____getattr____call__小结使用枚举类exercise小结使用元类type()metaclass 面向对象高级...
本文实例讲述了Python面向对象之类的内置attr属性。分享给大家供大家参考,具体如下: 这个比较简单。 代码示例: # -*- coding:utf-8 -*- #! python3 class Foo: x = 1; def __init__(self,y): self.y = y; ...
主要介绍了Python类中方法getitem和getattr详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
简介 在Java中我们可以通过反射来根据类名创建类实例,那么在Python我们怎么实现类似功能呢? 其实在Python有一个builtin函数import,我们可以使用这个函数来... class_meta = getattr(module_meta, class_name) obj =
PEP引入了两种特殊方法__class_getitem __()和__mro_entries__,这些方法现在被大多数类和特殊构造用于输入。结果,不同类型的各种操作的速度提高了7倍,可以使用泛型而没有元类冲突,并且修改了类型模块中长期...
本文实例讲述了Python实现计算对象的内存大小。分享给大家供大家参考,具体如下: 一般的sys.getsizeof()显示不了复杂的字典。 查看类中的内容: def dump(obj): for attr in dir(obj):#dir显示类的所有方法 ...