[英]Difficulties with re-using a variable
here is a part of my code : 这是我的代码的一部分:
class projet(object):
def nameCouche(self):
valLissage = float(ui.valLissage.displayText())
return (valLissage)
valCouche = nameCouche() # asks for a positional argument but 'self' doesnt work
def choixTraitement(self):
ui.okLissage.clicked.connect(p.goLissage)
def goLissage(self, valCouche):
if ui.chkboxLissage.isChecked():
print(valCouche) # result is False
os.system(r'"C:\Program Files\FME\fme.exe" D:\Stelios\..... --MAX_NUM_POINTS {0}'.format(valCouche))
So I would like to use valCouche
in goLissage
method but it doesnt work. 所以我想在goLissage
方法中使用valCouche
,但是它不起作用。 I thought that valCouche
would have the argument of valLissage
but instead it gives False as a value. 我认为valCouche
会有的说法valLissage
而是它提供虚假的价值。 I've tried different alternatives but still doesnt work. 我尝试了其他替代方法,但仍然无法正常工作。
You've got multiple problems here. 您在这里遇到了多个问题。
First, if you write this in the middle of a class definition: 首先,如果您在类定义的中间编写此代码:
valCouche = nameCouche()
... you're creating a class attribute, which is shared by all instances, not a normal instance attribute. ...您正在创建一个类属性,所有实例都共享该属性,而不是普通实例属性。
Also, you're running this at class definition time. 另外,您正在类定义时运行它。 That means there is no self
yet--there aren't any instances yet to be self
--so you can't call a method like nameCouche
, because you don't have anything to call it on. 这意味着, 没有 self
,但-没有任何情况有待self
-因此你不能调用的方法等nameCouche
,因为你没有什么调用它。
What you want to do is call the method at instance initialization time, on the instance being initialized, and store the return value in an instance attribute: 您要做的是在实例初始化时在要初始化的实例上调用方法,并将返回值存储在实例属性中:
def __init__(self):
self.valCouche = self.nameCouche()
Then, when you want to access this value in another method later, you have to access it as self.valCouche
. 然后,当您以后想要使用其他方法访问此值时,必须以self.valCouche
访问它。
If you make those changes, it will work . 如果您进行了这些更改,它将起作用 。 But your object model still doesn't make much sense. 但是您的对象模型仍然没有多大意义。 Why is nameCouche
a method when it doesn't have anything to do with the object, and doesn't access any of its attributes? 当nameCouche
与该对象没有任何关系,并且不访问其任何属性时,为什么要使用它呢? Maybe it makes sense as a @staticmethod
, but really, I think it makes more sense just as a plain function outside the class. 也许它作为@staticmethod
,但是实际上,我认为就像在类外的普通函数一样有意义。 In fact, none of the code you've written seems to have anything to do with the class. 事实上, 没有你写的代码似乎有什么关系类。
This kind of cram-everything-into-the-class design is often a sign that you're trying to write Java code in Python, and haven't yet really understood how Python does OO. 这种将一切都塞入课堂的设计通常表明您正在尝试用Python编写Java代码,但还没有真正了解Python如何实现OO。 You might want to read a good tutorial on Python classes. 您可能想阅读有关Python类的优秀教程。 But briefly: if you're writing a class just to have somewhere to dump a bunch of vaguely-related functions, what you want is a module, not a class. 但简单来说:如果您要编写一个类只是为了在某个地方转储一些模糊的相关函数,那么您想要的是模块而不是类。 If you have some reason to have instances of that class, and the functions all act on the data of each instance, then you want a class. 如果您出于某种原因拥有该类的实例,并且所有功能都作用于每个实例的数据,则您需要一个类。
You have to declare variabile in the __init__
method (constructor) and then use it in your code 您必须在__init__
方法(构造函数)中声明可变性,然后在代码中使用它
ex: 例如:
class projet(object):
def __init__(self):
self.valCouche = ''
def nameCouche(self):
valLissage = float(ui.valLissage.displayText())
return (valLissage)
def choixTraitement(self):
ui.okLissage.clicked.connect(p.goLissage)
def goLissage(self, valCouche):
if ui.chkboxLissage.isChecked():
self.valCouche = self.nameCouche()
print(self.valCouche) # result is False
os.system(r'"C:\Program Files\FME\fme.exe" D:\Stelios\..... --MAX_NUM_POINTS {0}'.format(self.valCouche))
you have to define an initialization function: def__init__(self) defining valCouche as an instance attribute make it accessible on all the method so we have the following 您必须定义一个初始化函数:def__init __(self)将valCouche定义为实例属性,使其可以在所有方法上访问,因此我们需要以下内容
class projet(object):
def __init__(self):
self.valCouche = ''
def nameCouche(self):
self.valCouche = float(ui.valLissage.displayText())
@staticmethod #here there is no need for self so it is a method of class
def choixTraitement():
ui.okLissage.clicked.connect(p.goLissage)
def goLissage(self):
if ui.chkboxLissage.isChecked():
print(self.valCouche) # result is False
os.system(r'"C:\Program Files\FME\fme.exe" D:\Stelios\..... --MAX_NUM_POINTS {0}'.format(self.valCouche))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.