I've just started dipping my feet into OOP.
Is it considered bad practice to have classes that reference attributes that depend on another function being called and thus may not exist (version 1)? I'm self-taught so trying to gauge what is considered better quality. Any mistakes are easily fixed by just making sure I call functions in order?
I could make the function add_extra_attribute
explicitly take a variable (something like version 2), but this would be more verbose and making add_attribute
and add_new_attribute
work together could be more confusing.
Cheers for reading!
Version 1
class test:
def __init__(self):
self.attribute = 'hello'
def add_attribute(self):
self.new_attribute = self.attribute + 'there'
def add_extra_attribute(self):
self.extra_attribute = self.new_attribute + 'you'
t = test()
t.add_attribute() #if I comment this out it will AttributeError
t.add_extra_attribute()
Version 2
class test:
def __init__(self):
self.attribute = 'hello'
def add_attribute(self):
self.new_attribute = self.attribute + 'there'
def add_extra_attribute(self, new_attribute):
self.extra_attribute = self.attribute + new_attribute + 'you'
t = test()
t.add_attribute()
t.add_extra_attribute('there')