Python 对多个对象使用固定属性

Python 对多个对象使用固定属性,python,class,Python,Class,很抱歉这个标题让人困惑,因为我真的不知道如何描述这个问题。 我将试着用一个例子来解释 说我想写一个班级 class Line: def __init__(self, x1, y1, x2, x3): self.start = (x1, y1) self.end = (x2, y2) def length(self, metric): # return the length of this line using the input m

很抱歉这个标题让人困惑,因为我真的不知道如何描述这个问题。 我将试着用一个例子来解释

说我想写一个班级

class Line:
    def __init__(self, x1, y1, x2, x3):
        self.start = (x1, y1)
        self.end = (x2, y2)
    def length(self, metric):
        # return the length of this line using the input metric
此处
metric
是平面上的一个度量(它可能是函数或表等,此处不重要)

现在我想做一些类似的事情

def findLine(metric):
    l1 = Line(0,0,1,1)
    l2 = Line(0,0,2,2)
    # just an example, I may need to create a lot of lines then compare their length
    if l1.length(metric)>l2.length(metric):
        return l1
我正在寻找一种方法,为
findLine
中使用的所有行设置
metric
的默认值

所以我可以在
findLine
中调用
l1.length()>l2.length()

此外,数据
度量
可能存储在一个大数据框中。我认为把它们放在每一行可能不好

对不起,弄糊涂了。我只是想找到一种简化代码的方法


我应该补充一点,在我的代码中,有5或6个这样的参数,而不仅仅是一个

这就是我想找到一种方法来避免每次都写入所有参数的原因


谢谢

可以使用类属性:

class Line:
    metric = (1,2)

    def __init__(self, x1, y1, x2, y2):
        self.start = (x1, y1)
        self.end = (x2, y2)

    def length(self):

        return Line.metric+self.start
# return the length of this line using the input metric

l = Line(1,2,3,4)
m = Line(4,5,6,7)

print(l.metric)
print(m.metric)
print(l.length())
print(m.length())
class Line:
    def __init__(self, x1, y1, x2, y2, metric=None):
        self.start = (x1, y1)
        self.end = (x2, y2)
        self.metric = metric

    def length(self):
        if self.metric is not None:
            return self.metric + self.start
        else:
            return 'Line has no metric'
    # return the length of this line using the input metric


metric1 = (1,2)
metric2 = (2,3)
l = Line(1, 2, 3, 4, metric=metric1)
m = Line(4, 5, 6, 7, metric=metric2)
n = Line(8, 9, 10, 11)

print(l.metric)
print(m.metric)
print(l.length())
print(m.length())
print(n.length())
这属于类而不是实例。对于
度量
,任何实例都将具有相同的值。您可以通过调用原始的
类而不是
self
来访问实例中
metric
的值

如果希望
metric
仅在某些实例中出现,而不在其他实例中出现,则最好将其添加为实例属性:

class Line:
    metric = (1,2)

    def __init__(self, x1, y1, x2, y2):
        self.start = (x1, y1)
        self.end = (x2, y2)

    def length(self):

        return Line.metric+self.start
# return the length of this line using the input metric

l = Line(1,2,3,4)
m = Line(4,5,6,7)

print(l.metric)
print(m.metric)
print(l.length())
print(m.length())
class Line:
    def __init__(self, x1, y1, x2, y2, metric=None):
        self.start = (x1, y1)
        self.end = (x2, y2)
        self.metric = metric

    def length(self):
        if self.metric is not None:
            return self.metric + self.start
        else:
            return 'Line has no metric'
    # return the length of this line using the input metric


metric1 = (1,2)
metric2 = (2,3)
l = Line(1, 2, 3, 4, metric=metric1)
m = Line(4, 5, 6, 7, metric=metric2)
n = Line(8, 9, 10, 11)

print(l.metric)
print(m.metric)
print(l.length())
print(m.length())
print(n.length())

你可以在你创建的每一行中输入“metric”。这个问题和标题一样令人困惑。您是否在寻找默认属性值,例如
def length(self,metric='xxx'):
?我已经编辑了这个问题,希望它更清晰。谢谢。您说:
数据度量可能存储在一个大数据框中
,但是如果每个
都有它自己的引用,您只会增加一个额外的引用,而不是增加
。谢谢!您认为可以在不将度量存储到每一行的情况下执行此操作吗?可以,但是没有
度量的行将无法使用
长度方法。你同意吗?这正是@user32882所做的:有一个度量,但每个实例都可以看到那一个度量。我认为这样做,它会复制一个新度量并将其存储到每一行中。但它实际上只会引用原点度量,对吗?谢谢。那你为什么不把
度量
传递到你的
\uuuu init\uuuu
方法中呢?这将使其成为实例属性。我将编辑我的答案以显示我的意思