简体   繁体   English

如何在 python 中的 class 中定义全局变量

[英]How to define global variable in class in python

I want to take a variable outside the class but not outside of the file.我想在 class 之外取一个变量,但不在文件之外。 I have a condition outside the class but I have to use it in the class too.我在 class 之外有一个条件,但我也必须在 class 中使用它。 Have can i do that?我可以这样做吗?

This is the sample to try if it is working.如果它有效,这是尝试的示例。 I want to erase the input part and do with global variable.我想擦除输入部分并使用全局变量。

class ComplexMethods:
    ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")
    if ask == "real and imaginary parts":

I tried this one but not working.我试过这个但不工作。 It gives name "ask" not defined.它给出了未定义的名称“询问”。

class ComplexMethods:
     global ask
     if ask == "real and imaginary parts":

This is the outside of class.这是 class 的外部。

ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")
if ask == "real and imaginary parts":
    firstcomplexreal = float(input("Enter real part of first complex number: "))
    firstcompleximaginary = float(input("Enter imaginary part of first complex number: "))
    secondcomplexreal = float(input("Enter real part of second complex number: "))
    secondcompleximaginary = float(input("Enter imaginary part of second complex number: "))
    complexnumbers = ComplexMethods(firstcomplexreal, firstcompleximaginary, secondcomplexreal,
                                    secondcompleximaginary)

If you just want to define your variable outside of the class you don't need to use the global keyword unless you plan on modifying it.如果您只想在 class 之外定义变量,则不需要使用global关键字,除非您打算修改它。 If you just want to read the variable and not modify it you could just do something like.如果您只想读取变量而不修改它,您可以执行类似的操作。

ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")

class ComplexMethods:
    if ask == "real and imaginary parts":
        pass

if ask == "real and imaginary parts":
    firstcomplexreal = float(input("Enter real part of first complex number: "))
    firstcompleximaginary = float(input("Enter imaginary part of first complex number: "))
    secondcomplexreal = float(input("Enter real part of second complex number: "))
    secondcompleximaginary = float(input("Enter imaginary part of second complex number: "))
    complexnumbers = ComplexMethods(firstcomplexreal, firstcompleximaginary, secondcomplexreal,
                                    secondcompleximaginary)

I'm not exactly sure what you are trying to do but perhaps you just need to have global ask above your ask=input("") outside the class.我不确定您要做什么,但也许您只需要在 class 之外的ask=input("")上方进行global ask

I found the solution.我找到了解决方案。 Thank you from Barmar.来自巴尔玛的感谢。 It worked.有效。

ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")


class ComplexMethods:
    global ask
    if ask == "real and imaginary parts":

if you're referring to a static, something available from Class.val and instance.val referencing the same thing;如果您指的是 static,则可以从Class.valinstance.val获得相同的东西; you can declare one in the class body您可以在 class 正文中声明一个

class A:
  val = None # Static variable
  def __init__(self, x, y):
    # Instance variables
    self.x = x
    self.y = y

inst_1 = A(2, 3)
inst_2 = A("a", "b")

print(A.val)
# static can't be modified via instance, only A
A.val = "abcd"
print(inst_2.val) # static modified across As

# Unique to each instance
print(inst_1.x)
print(inst_2.x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM