问题
- 为什么类方法在实例化时对普通方法视而不见?你知道吗
- 有没有一种方法可以传递
self
以允许类方法在实例化时参见普通方法?你知道吗
观察
非实例化类方法
- 当使用类方法时,对任何属性的修改都将保持不变。你知道吗
- 我希望每次使用class函数时,在classmethod运行时只临时存储属性
实例化的类方法
- Classmethods不能使用从普通方法设置的任何变量
- 即使类是实例化的,classmethods仍然对普通方法中所做的任何事情完全视而不见!你知道吗
- 这是意想不到的行为。我希望classmethods在实例化之后可以像普通方法一样工作!你知道吗
示例代码
def fmt_atts(self, att):
"""A simple formatter"""
if hasattr(self, att): print '\t self.{:15s} = {}'.format(att, getattr(self, att))
else: print "\t self.{:15s} does not exist".format(att)
def _report_vals(func):
"""This just reports the current state of values"""
@wraps(func)
def wrapper(*args, **kwargs):
self = args[0]
print '> calling func: {} :: {}'.format(func.__name__, func.__doc__)
res = func(*args, **kwargs)
for att in self.all_attrs:
fmt_atts(self, att)
print '> end func: {}'.format(func.__name__)
return res
return wrapper
class Example(object):
all_attrs = ['att_class', 'att_clsFnSet', 'att_initSet']
att_class = 'set_in_class'
@_report_vals
def __init__(self):
"""setting an attribute and calling self.test2"""
self.att_initSet = 'set_in_init'
self.set_atts_in_classmethod()
@classmethod
@_report_vals
def set_atts_in_classmethod(cls):
"""Sets attributes from within a classmethod"""
cls.att_class = 'set_in_classmethod'
cls.att_clsFnSet = 'set_in_classmethod'
@classmethod
@_report_vals
def view_atts_from_classmethod(cls):
"""View attributes from a classmethod"""
pass
@_report_vals
def view_atts_from_method(self):
"""View attributes from a normal method"""
pass
@_report_vals
def set_atts_in_method(self):
"""Sets attributes from within a normal method"""
self.att_class = 'set_in_method'
self.att_clsFnSet = 'set_in_method'
if __name__ == '__main__':
print '__without init'
print '> calling `Example.att_class` directly'
fmt_atts(Example, 'att_class')
print '# comment: notice that `A.att_class` has a persisting value'
Example.set_atts_in_classmethod()
Example.view_atts_from_classmethod()
print '\n__ post init: ex = Example()'
ex = Example()
print '# comment: notice that `self.att_initSet` has been set but not accessible from classmethod'
ex.set_atts_in_classmethod()
ex.view_atts_from_classmethod()
print '# comment: notice that `self.att_initSet` was set in __init__ but not avialable!'
print '# comment: however, `self.att_class` was set in another classmethod but *IS* accessible'
ex.view_atts_from_method()
print '# comment: notice that `self.att_initSet` is accessible from a normal method'
ex.set_atts_in_method()
ex.view_atts_from_classmethod()
print '# comment: It appears that classmethods can only access attributes set by other classmethods'
print '# comment: even when instanciated'
输出示例
__without init
> calling `Example.att_class` directly
self.att_class = set_in_class
# comment: notice that `A.att_class` has a persisting value
> calling func: set_atts_in_classmethod :: Sets attributes from within a classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet does not exist
> end func: set_atts_in_classmethod
> calling func: view_atts_from_classmethod :: View attributes from a classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet does not exist
> end func: view_atts_from_classmethod
__ post init: ex = Example()
> calling func: __init__ :: setting an attribute and calling self.test2
> calling func: set_atts_in_classmethod :: Sets attributes from within a classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet does not exist
> end func: set_atts_in_classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet = set_in_init
> end func: __init__
# comment: notice that `self.att_initSet` has been set but not accessible from classmethod
> calling func: set_atts_in_classmethod :: Sets attributes from within a classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet does not exist
> end func: set_atts_in_classmethod
> calling func: view_atts_from_classmethod :: View attributes from a classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet does not exist
> end func: view_atts_from_classmethod
# comment: notice that `self.att_initSet` was set in __init__ but not avialable!
# comment: however, `self.att_class` was set in another classmethod but *IS* accessible
> calling func: view_atts_from_method :: View attributes from a normal method
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet = set_in_init
> end func: view_atts_from_method
# comment: notice that `self.att_initSet` is accessible from a normal method
> calling func: set_atts_in_method :: Sets attributes from within a normal method
self.att_class = set_in_method
self.att_clsFnSet = set_in_method
self.att_initSet = set_in_init
> end func: set_atts_in_method
> calling func: view_atts_from_classmethod :: View attributes from a classmethod
self.att_class = set_in_classmethod
self.att_clsFnSet = set_in_classmethod
self.att_initSet does not exist
> end func: view_atts_from_classmethod
# comment: It appears that classmethods can only access attributes set by other classmethods
# comment: even when instantiated
Tags:
为什么类方法在实例化时对普通方法视而不见?你知道吗
类(或静态)方法并不意味着作用于实例。它们是类的所有实例所共有的方法。因此,类方法对一个特定实例进行操作是不符合逻辑的。你知道吗
这些通常是实用功能。例如,
Vector
类可以将dot_product
实现为类方法,而不是实例方法。这是有道理的,因为这样的操作涉及多个实例。你知道吗有没有方法传递self,让classmethods在实例化时看到普通方法?你知道吗
是的,确实有,尽管这说不通。你知道吗
首先,类方法能够查看实例方法。例如:
输出:
让我们更进一步,稍微改变一下
make_speak
方法:输出:
这个输出揭示的是,
cls.speak
引用的speak
方法实际上对应于一个没有附加到任何实例的speak
方法。调用该函数时可以更清楚地看到这一点:输出:
会引发
TypeError
,因为Foo
类中的speak
方法需要一个参数,而该参数不会被传递。你知道吗解决方法
免责声明:以下不是一个好的解决方案。
speak
方法需要一个实例作为要运行的参数。那么,就给它:输出:
但是通过这样做,您希望
make_speak
方法作用于特定的Foo
实例,以便调用其speak
方法。你知道吗这相当于直接调用
foo.speak
。你知道吗这个例子可能显得明显而乏味。然而,它强调了这样一个事实:如果一个类方法只取一个实例作为参数(可能还有其他不是这个类实例的东西),那么它就相当于一个实例方法。你知道吗
相关问题 更多 >
编程相关推荐