【发布时间】:2017-10-16 17:25:11
【问题描述】:
问题
- 为什么类方法在实例化时对普通方法视而不见?
- 有没有办法通过
self让类方法在实例化时查看普通方法?
观察
未实例化的类方法
- 使用类方法时,任何属性的修改都会持续存在。
- 我希望每次使用类函数时只在类方法运行时临时存储属性
实例化的类方法
- 类方法不能使用任何通过普通方法设置的变量
- 即使类被实例化,类方法仍然对普通方法中所做的任何事情完全视而不见!
- 这是意外行为。我希望类方法在实例化后表现得像普通方法!
示例代码
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
【问题讨论】:
-
从我的手机发帖 - 整理代码语法!
-
您似乎对类方法的作用以及类属性与实例属性有一些误解。但是在当前状态下阅读您的代码并不容易。
-
是的,非常抱歉我的工作场所阻塞了堆栈溢出。我不得不将我的问题通过电子邮件发送到我的手机并在此处进行编辑。我想我有办法解决这个格式问题
-
语法固定:)
标签: python class methods instantiation class-method