I want to access one class attribute s
and split it into 2 strings in another class. Any tips?
class lyssna:
def du():
s = '5-22'
class Fixish():
strt, stpp = lyssna.s.split('-')
I want to access one class attribute s
and split it into 2 strings in another class. Any tips?
class lyssna:
def du():
s = '5-22'
class Fixish():
strt, stpp = lyssna.s.split('-')
lyssna
is a class whose only (manually declared) attribute is du
, a method. It's expected that s
is not available from lyssna
as s
is a variable whose scope is restricted to du
.
I don't know exactly why you want to define classes to do that job, because you can simply define a function as follows:
def split_hyphen(text):
return text.split('-', maxsplit=1)
If the text
of interest is an attribute of another class, you can access it using:
class A:
text_of_intereset = '1-2'
split_hyphen(A.text_of_interest)
If it is an attribute of an instance of another class:
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)