我想访问一个类属性s并将其拆分为另一个类中的 2 个字符串。有小费吗?
class lyssna:
def du():
s = '5-22'
class Fixish():
strt, stpp = lyssna.s.split('-')
我想访问一个类属性s并将其拆分为另一个类中的 2 个字符串。有小费吗?
class lyssna:
def du():
s = '5-22'
class Fixish():
strt, stpp = lyssna.s.split('-')
lyssna是一个类,其唯一(手动声明的)属性是du, 一个方法。预计它s不能从lyssnaass是一个范围被限制为du.
我不知道你为什么要定义类来完成这项工作,因为你可以简单地定义一个函数,如下所示:
def split_hyphen(text):
return text.split('-', maxsplit=1)
如果text感兴趣的是另一个类的属性,您可以使用以下方法访问它:
class A:
text_of_intereset = '1-2'
split_hyphen(A.text_of_interest)
如果它是另一个类的实例的属性:
class A:
def __init__(self, text_as_parameter):
self.text_of_interest = text_as_parameter
# Create an instance of A
a = A('1-2')
split_hyphen(a.text_of_interest)