Python 添加类方法

Python 添加类方法,python,python-3.x,Python,Python 3.x,我编写了以下正确运行的代码: import math class Point: """Two-Dimensional Point(x, y)""" def __init__(self, x=0, y=0): # Initialize the Point instance self.x = x self.y = y def __iter__(self): yield self.x yie

我编写了以下正确运行的代码:

import math

class Point:
    """Two-Dimensional Point(x, y)"""
    def __init__(self, x=0, y=0):
        # Initialize the Point instance
        self.x = x
        self.y = y

    def __iter__(self):
         yield self.x
         yield self.y

    def __add__(self, other):
        addedx = self.x + other.x
        addedy = self.y + other.y
        return Point(addedx, addedy)

    def __mul__(self, other):
        mulx = self.x * other
        muly = self.y * other
        return Point(mulx, muly)

    def __rmul__(self, other):
        mulx = self.x * other
        muly = self.y * other
        return Point(mulx, muly)

    @classmethod
    def from_tuple(


    @property
    def magnitude(self):
        # """Return the magnitude of vector from (0,0) to self."""
        return math.sqrt(self.x ** 2 + self.y ** 2)


    def distance(self, self2):
         return math.sqrt((self2.x - self.x) ** 2 + (self2.y - self.y) ** 2)

    def __str__(self):
        return 'Point at ({}, {})'.format(self.x, self.y)

    def __repr__(self):
        return "Point(x={},y={})".format(self.x, self.y)
我想向Point类添加一个名为from_tuple的@classmethod,该类允许从包含x和y值的元组创建点实例。我想让它这样的元组输入是必需的。我不知道如何做这样的事情。以下是预期输出:

location = 2, 3
print(location)
    (2, 3)
point = Point.from_tuple(location)
print(point)
    Point(x=2, y=3)
point = Point.from_tuple()
Traceback (most recent call last):
[...]
TypeError: from_tuple() missing 1 required positional argument: 'coords'
只是

@classmethod
def from_tuple(cls, location):
    return cls(*location)
只是

@classmethod
def from_tuple(cls, location):
    return cls(*location)

classmethod
与实例方法非常相似,只是:

  • 您可以在类本身上调用它,而无需获得需要额外参数的“unbound方法”,并且
  • 无论是在类本身上还是在实例上调用它,都会得到类对象作为第一个参数
传统上,您可以将该参数命名为
cls
。因此:

@classmethod
def from_tuple(cls, tup):
    x, y = tup
    return cls(x, y)

您可能想知道为什么需要
cls
。毕竟,你不能把它写成一个
staticmethod

@staticmethod
def from_tuple(tup):
    x, y = tup
    return Point(x, y)

是的,你可以,但这不利于继承。如果我创建一个子类
Point
,并从元组(2,3)调用
MyPoint。通过使用
classmethod
并构造
cls
的实例而不是硬编码
Point
,我得到了
MyPoint
,因为
cls
MyPoint

一个
classmethod
与实例方法非常相似,除了:

  • 您可以在类本身上调用它,而无需获得需要额外参数的“unbound方法”,并且
  • 无论是在类本身上还是在实例上调用它,都会得到类对象作为第一个参数
传统上,您可以将该参数命名为
cls
。因此:

@classmethod
def from_tuple(cls, tup):
    x, y = tup
    return cls(x, y)

您可能想知道为什么需要
cls
。毕竟,你不能把它写成一个
staticmethod

@staticmethod
def from_tuple(tup):
    x, y = tup
    return Point(x, y)

是的,你可以,但这不利于继承。如果我创建一个子类
Point
,并从元组(2,3)
调用
MyPoint。通过使用
classmethod
并构造
cls
的实例而不是硬编码
Point
,我得到了
MyPoint
,因为
cls
MyPoint

,谢谢您的解释。接下来的问题是,如何创建一个允许从元组更新点实例的x,y值的方法。我想这样做,如果point=point(3,4),我说例如point.loc_tuple((5,6)),那么输出将是point(x=5,y=6)。@user10200421您想修改它吗?然后编写一个这样做的方法:
def loc\u tuple(self,tup):self.x,self.y=tup
。感谢您的解释。接下来的问题是,如何创建一个允许从元组更新点实例的x,y值的方法。我想这样做,如果point=point(3,4),我说例如point.loc_tuple((5,6)),那么输出将是point(x=5,y=6)。@user10200421您想修改它吗?然后只需编写一个这样做的方法:
def loc\u tuple(self,tup):self.x,self.y=tup