Python类方法的示例用例是什么?

时间:2011-04-21 01:19:18

标签: python class class-method

我已阅读What are Class methods in Python for?但该帖子中的示例很复杂。我正在寻找Python中类方法的特定用例的清晰,简单,简单的例子。

您能说出一个小的,具体的示例用例,其中Python类方法将是该工作的正确工具吗?

6 个答案:

答案 0 :(得分:46)

辅助初始化方法:

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):
       ...

答案 1 :(得分:28)

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

所以dict()当然会调用dict.__new__,但有时还有另一种方便的方法来制作词类dict.fromkeys()

例如

>>> dict.fromkeys("12345")
{'1': None, '3': None, '2': None, '5': None, '4': None}

答案 2 :(得分:18)

我不知道,有点像命名构造函数方法吗?

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

答案 3 :(得分: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')

答案 4 :(得分:10)

使用@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是一个定义类方法from_square的抽象类,因为Shape没有真正定义,它实际上并不知道如何从{{1}派生自己所以它只返回该类的默认实例。

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

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) 来完成同样的多态行为:

@staticmethod

答案是你可以,但是你没有得到继承的好处,因为必须在方法中明确地调出class Circle(Shape): def __init__(self, radius=10): self.radius = radius @staticmethod def from_square(square): return Circle(radius=square.length/2) 。这意味着如果我在没有覆盖的情况下从继承的类中调用它,我每次都会得到Circle

注意当我定义另一个没有任何自定义from_square逻辑的形状类时获得的是什么:

Circle

在这里,您可以保留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

答案 5 :(得分:-3)

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)