78

我已阅读Python 中的类方法是做什么用的?但是该帖子中的示例很复杂。我正在为 Python 中的类方法的特定用例寻找一个清晰、简单、简单的示例。

你能举出一个小的、具体的示例用例,其中 Python 类方法将是完成这项工作的正确工具吗?

4

6 回答 6

57

初始化的辅助方法:

class MyStream(object):

    @classmethod
    def from_file(cls, filepath, ignore_comments=False):    
        with open(filepath, 'r') as fileobj:
            for obj in cls(fileobj, ignore_comments):
                yield obj

    @classmethod
    def from_socket(cls, socket, ignore_comments=False):
        raise NotImplemented # Placeholder until implemented

    def __init__(self, iterable, ignore_comments=False):
       ...
于 2011-04-21T01:40:47.337 回答
33

Well__new__是一个非常重要的类方法。这是实例通常来自的地方

当然可以dict()调用dict.__new__,但是有时还有另一种方便的方法可以制作 dicts,即 classmethoddict.fromkeys()

例如。

>>> dict.fromkeys("12345")
{'1': None, '3': None, '2': None, '5': None, '4': None}
于 2011-04-21T01:48:27.257 回答
21

我不知道,类似于命名构造方法?

class UniqueIdentifier(object):

    value = 0

    def __init__(self, name):
        self.name = name

    @classmethod
    def produce(cls):
        instance = cls(cls.value)
        cls.value += 1
        return instance

class FunkyUniqueIdentifier(UniqueIdentifier):

    @classmethod
    def produce(cls):
        instance = super(FunkyUniqueIdentifier, cls).produce()
        instance.name = "Funky %s" % instance.name
        return instance

用法:

>>> x = UniqueIdentifier.produce()
>>> y = FunkyUniqueIdentifier.produce()
>>> x.name
0
>>> y.name
Funky 1
于 2011-04-21T01:33:49.697 回答
17

使用 a 的最大原因@classmethod是在旨在被继承的备用构造函数中。这在多态性中非常有用。一个例子:

class Shape(object):
    # this is an abstract class that is primarily used for inheritance defaults
    # here is where you would define classmethods that can be overridden by inherited classes
    @classmethod
    def from_square(cls, square):
        # return a default instance of cls
        return cls()

请注意,这Shape是一个定义了 classmethod 的抽象类from_square,因为Shape没有真正定义,它并不真正知道如何从 a 派生自己,Square因此它只是返回该类的默认实例。

然后允许继承的类定义自己的此方法版本:

class Square(Shape):
    def __init__(self, side=10):
        self.side = side

    @classmethod
    def from_square(cls, square):
        return cls(side=square.side)


class Rectangle(Shape):
    def __init__(self, length=10, width=10):
        self.length = length
        self.width = width

    @classmethod
    def from_square(cls, square):
        return cls(length=square.side, width=square.side)


class RightTriangle(Shape):
    def __init__(self, a=10, b=10):
        self.a = a
        self.b = b
        self.c = ((a*a) + (b*b))**(.5)

    @classmethod
    def from_square(cls, square):
        return cls(a=square.length, b=square.width)


class Circle(Shape):
    def __init__(self, radius=10):
        self.radius = radius

    @classmethod
    def from_square(cls, square):
        return cls(radius=square.length/2)

该用法允许您以多态方式处理所有这些未实例化的类

square = Square(3)
for polymorphic_class in (Square, Rectangle, RightTriangle, Circle):
    this_shape = polymorphic_class.from_square(square)

您可能会说这一切都很好而且很花哨,但为什么我不能使用 as@staticmethod来完成同样的多态行为:

class Circle(Shape):
    def __init__(self, radius=10):
        self.radius = radius

    @staticmethod
    def from_square(square):
        return Circle(radius=square.length/2)

答案是你可以,但你没有得到继承的好处,因为Circle必须在方法中显式调用。这意味着如果我从继承的类中调用它而不覆盖,我仍然会Circle每次都得到。

请注意,当我定义另一个实际上没有任何自定义 from_square 逻辑的形状类时获得了什么:

class Hexagon(Shape):
    def __init__(self, side=10):
        self.side = side

    # note the absence of classmethod here, this will use from_square it inherits from shape

在这里您可以保留@classmethod未定义的内容,它将使用来自的逻辑,Shape.from_square同时保留谁cls是并返回适当的形状。

square = Square(3)
for polymorphic_class in (Square, Rectangle, RightTriangle, Circle, Hexagon):
    this_shape = polymorphic_class.from_square(square)
于 2017-07-17T18:04:56.790 回答
10

我发现我最常使用@classmethod将一段代码与一个类相关联,以避免创建一个全局函数,在我不需要类的实例来使用代码的情况下。

例如,我可能有一个数据结构,它仅在符合某种模式时才认为密钥有效。我可能想在课堂内外使用它。但是,我不想再创建另一个全局函数:

def foo_key_is_valid(key):
    # code for determining validity here
    return valid

我更愿意将此代码与其关联的类分组:

class Foo(object):

    @classmethod
    def is_valid(cls, key):
        # code for determining validity here
        return valid

    def add_key(self, key, val):
        if not Foo.is_valid(key):
            raise ValueError()
        ..

# lets me reuse that method without an instance, and signals that
# the code is closely-associated with the Foo class
Foo.is_valid('my key')
于 2011-04-21T01:39:18.857 回答
-4
in class MyClass(object):
    '''
    classdocs
    '''
    obj=0
    x=classmethod
    def __init__(self):
        '''
        Constructor
        '''
        self.nom='lamaizi'
        self.prenom='anas'
        self.age=21
        self.ville='Casablanca'
if __name__:
    ob=MyClass()
    print(ob.nom)
    print(ob.prenom)
    print(ob.age)
    print(ob.ville)
于 2015-02-09T19:52:43.773 回答