为什么类方法在实例化时对普通方法视而不见?

2024-10-03 09:12:30 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

  1. 为什么类方法在实例化时对普通方法视而不见?你知道吗
  2. 有没有一种方法可以传递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: infromselfviewmethodattattributesclass
1条回答
网友
1楼 · 发布于 2024-10-03 09:12:30

为什么类方法在实例化时对普通方法视而不见?你知道吗

类(或静态)方法并不意味着作用于实例。它们是类的所有实例所共有的方法。因此,类方法对一个特定实例进行操作是不符合逻辑的。你知道吗

这些通常是实用功能。例如,Vector类可以将dot_product实现为类方法,而不是实例方法。这是有道理的,因为这样的操作涉及多个实例。你知道吗

有没有方法传递self,让classmethods在实例化时看到普通方法?你知道吗

是的,确实有,尽管这说不通。你知道吗

首先,类方法能够查看实例方法。例如:

class Foo:
    def speak(self):
        print("Hello")

    @classmethod
    def make_speak(cls, instance):
        instance.speak()

foo = Foo()
Foo.make_speak(foo)

输出:

Hello

让我们更进一步,稍微改变一下make_speak方法:

@classmethod
def make_speak(cls):
    print(cls.speak)

Foo.make_speak()

输出:

<function Foo.speak at 0x000001E58331EF28>

这个输出揭示的是,cls.speak引用的speak方法实际上对应于一个没有附加到任何实例的speak方法。调用该函数时可以更清楚地看到这一点:

@classmethod
def make_speak(cls):
    cls.speak()

Foo.make_speak()

输出:

TypeError: speak() missing 1 required positional argument: 'self'

会引发TypeError,因为Foo类中的speak方法需要一个参数,而该参数不会被传递。你知道吗

解决方法

免责声明:以下不是一个好的解决方案。

speak方法需要一个实例作为要运行的参数。那么,就给它:

@classmethod
def make_speak(cls, instance):
    cls.speak(instance)

foo = Foo()
Foo.make_speak(foo)

输出:

Hello

但是通过这样做,您希望make_speak方法作用于特定的Foo实例,以便调用其speak方法。你知道吗

这相当于直接调用foo.speak。你知道吗


这个例子可能显得明显而乏味。然而,它强调了这样一个事实:如果一个类方法只取一个实例作为参数(可能还有其他不是这个类实例的东西),那么它就相当于一个实例方法。你知道吗

相关问题 更多 >