在python中如何从类中调用函数的值?

2024-03-29 09:59:55 发布

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

我有一个文件x.py和另一个文件y.py

我在x.py中有一个类x,它的函数定义如下:

@property
def x(self):
    return self._x

@x.setter
def x(self, x):
    self.update_x(x)

def xy(self, l):
    self._x = 100

现在我想在另一个文件中使用这个值y.py

我有:

from models.x import X # where X is the name of the class in  x.py

def functionX(self):
    y = # set this value as the value of x in function x from x.py

我该怎么做?你知道吗


Tags: 文件ofthe函数infrompyself
1条回答
网友
1楼 · 发布于 2024-03-29 09:59:55

类没有属性x。它所拥有的是一个属性方法x,并且方法只能在实例上调用(大多数情况下是这样)。你可以签出class methods and static methods,但它们不是你想要的)。你知道吗

所以在y.py中,需要类X的实例,可以通过执行foo = X()之类的操作来获得。然后可以参考foo.x。你知道吗

相关问题 更多 >