如何从 'self' 作为变量的函数中获取输入
How to get input from function with 'self' as variable
我想要一个函数,您可以在其中输入字符串,并且可以在 class 中重复使用该字符串。这是我尝试过的方法,但我无法让 variable
全局在 class
中的 function
之外使用它
test.py
from Moduletest import Test
Test().set_string("Hello")
模块test.py
class Test():
def set_string(self, target_string):
global string
string = target_string
print(string)
zeta = string
要操作测试 class 中定义的静态变量,您可以查看下面的应用程序。这样就可以观察到数据成员和静态变量的区别了。
class Test:
zeta = "" # static variable definition
@classmethod # constructor method
def __init__(self, string):
self.string = string
@classmethod # member method
def getString(self):
return self.string
@classmethod # member method
def setString(self, value):
self.string = value
@staticmethod # static method
def set_static_string(self, target):
self.setString(target) # set data member
Test.zeta = target # set static variable
firstTestObject = Test("test") # create object
Test.set_static_string(firstTestObject, "first") # set static variable
print("1: ", Test.zeta) # get static variable
print("2: ", firstTestObject.zeta) # get static variable
secondTestObject = Test("test") # create object
Test.set_static_string(secondTestObject, "second") # set static variable
print("3: ", Test.zeta) # get static variable
# The same value is printed as static variables are common to every object.
print("4: ", firstTestObject.zeta)
print("5: ", secondTestObject.zeta)
此应用程序产生以下输出:
1: first
2: first
3: second
4: second
5: second
这里不需要使用全局修饰符。您需要的是通过使用 self
对象来引用对象实例,如下所示:
class Test:
zeta = None
def __init__(self):
self.string = None
def set_string(self, target_string):
self.string = target_string
print(self.string)
Test.zeta = self.string
t = Test()
t.set_string('abc')
也许在 Moduletest.py:
中尝试这样做
class Test:
def __init__(self, target_string):
self.string = target_string
def getStr(self):
return self.target_string
然后在main.py:
from Moduletest import Test
x = Test("Hello")
print(x.getStr) #output: Hello
Self 是您在实例化 class 中引用函数变量的变量。
实例化您的 class 并使用构造函数传递参数的方法是这样的。
`class Test:`
`def __init__(self,arg1)`
`self.var1 = arg1`
你像这样实例化 class 测试 my_instance=Test("my_string")
您可以像这样通过 .
访问 var1。 my_instance.var1
通常强烈建议不要在 python 中创建全局变量。我建议使用 class 的实例,然后将字符串分配给 class.
的属性
class Test:
my_string: str
def__init__(self, target_string):
self.my_string = target_string
def get_string(self):
return self.my_string
然后你可以这样访问my_string
my_class = Test('Hello World')
the_string = my_class.get_string()
print(the_string) # Prints 'Hello World'
# Or like this
print(my_class.my_string) # Prints 'Hello World' as well
在此处提问时,请描述您想要实现的目标。在这种情况下,您应该说明为什么要尝试使字符串成为全局字符串,以便其他人可能会推荐替代方案,或者更好地理解问题。
根据 OP 的评论更新:
在 python 中很少有使用全局变量的用例。我建议将字符串存储为 class 实例变量,而不是将字符串存储为全局变量。您可以阅读更多关于 class 实例变量 here.
class Test:
my_string: str
# You could use a dunder init method like this
def __init__(self, target_string):
""" This is the function that is ran upon Invoking the
class as an instance """
self.my_string = target_string
# Or you could not use the dunder init method and just have a class method like this.
# This way of doing so is not recommended though
def set_string(self, target_string):
""" Sets class instance variable 'mystring' to that of the 'target_string' parameter """
self.my_string = target_string
# Then to set and retrieve the string from the class instance
# If using the __init__ method
tester_cls = Test('I am the target string')
print(tester_cls.my_string) # Prints 'I am the target string'
# or if using the set_string method
tester_cls = Test()
tester_cls.set_string('I am the target string')
print(tester_cls.my_string) # Prints 'I am the target string'
我想要一个函数,您可以在其中输入字符串,并且可以在 class 中重复使用该字符串。这是我尝试过的方法,但我无法让 variable
全局在 class
function
之外使用它
test.py
from Moduletest import Test
Test().set_string("Hello")
模块test.py
class Test():
def set_string(self, target_string):
global string
string = target_string
print(string)
zeta = string
要操作测试 class 中定义的静态变量,您可以查看下面的应用程序。这样就可以观察到数据成员和静态变量的区别了。
class Test:
zeta = "" # static variable definition
@classmethod # constructor method
def __init__(self, string):
self.string = string
@classmethod # member method
def getString(self):
return self.string
@classmethod # member method
def setString(self, value):
self.string = value
@staticmethod # static method
def set_static_string(self, target):
self.setString(target) # set data member
Test.zeta = target # set static variable
firstTestObject = Test("test") # create object
Test.set_static_string(firstTestObject, "first") # set static variable
print("1: ", Test.zeta) # get static variable
print("2: ", firstTestObject.zeta) # get static variable
secondTestObject = Test("test") # create object
Test.set_static_string(secondTestObject, "second") # set static variable
print("3: ", Test.zeta) # get static variable
# The same value is printed as static variables are common to every object.
print("4: ", firstTestObject.zeta)
print("5: ", secondTestObject.zeta)
此应用程序产生以下输出:
1: first
2: first
3: second
4: second
5: second
这里不需要使用全局修饰符。您需要的是通过使用 self
对象来引用对象实例,如下所示:
class Test:
zeta = None
def __init__(self):
self.string = None
def set_string(self, target_string):
self.string = target_string
print(self.string)
Test.zeta = self.string
t = Test()
t.set_string('abc')
也许在 Moduletest.py:
中尝试这样做class Test:
def __init__(self, target_string):
self.string = target_string
def getStr(self):
return self.target_string
然后在main.py:
from Moduletest import Test
x = Test("Hello")
print(x.getStr) #output: Hello
Self 是您在实例化 class 中引用函数变量的变量。
实例化您的 class 并使用构造函数传递参数的方法是这样的。
`class Test:`
`def __init__(self,arg1)`
`self.var1 = arg1`
你像这样实例化 class 测试 my_instance=Test("my_string")
您可以像这样通过 .
访问 var1。 my_instance.var1
通常强烈建议不要在 python 中创建全局变量。我建议使用 class 的实例,然后将字符串分配给 class.
的属性class Test:
my_string: str
def__init__(self, target_string):
self.my_string = target_string
def get_string(self):
return self.my_string
然后你可以这样访问my_string
my_class = Test('Hello World')
the_string = my_class.get_string()
print(the_string) # Prints 'Hello World'
# Or like this
print(my_class.my_string) # Prints 'Hello World' as well
在此处提问时,请描述您想要实现的目标。在这种情况下,您应该说明为什么要尝试使字符串成为全局字符串,以便其他人可能会推荐替代方案,或者更好地理解问题。
根据 OP 的评论更新: 在 python 中很少有使用全局变量的用例。我建议将字符串存储为 class 实例变量,而不是将字符串存储为全局变量。您可以阅读更多关于 class 实例变量 here.
class Test:
my_string: str
# You could use a dunder init method like this
def __init__(self, target_string):
""" This is the function that is ran upon Invoking the
class as an instance """
self.my_string = target_string
# Or you could not use the dunder init method and just have a class method like this.
# This way of doing so is not recommended though
def set_string(self, target_string):
""" Sets class instance variable 'mystring' to that of the 'target_string' parameter """
self.my_string = target_string
# Then to set and retrieve the string from the class instance
# If using the __init__ method
tester_cls = Test('I am the target string')
print(tester_cls.my_string) # Prints 'I am the target string'
# or if using the set_string method
tester_cls = Test()
tester_cls.set_string('I am the target string')
print(tester_cls.my_string) # Prints 'I am the target string'