I need a approach to this, it's quite simple, but something don't seem right.
I have my MyClass
which has foo
and bar
, that not always are instantiated, and I have this property baz
, that will compose of foo
and
bar.
class MyClass(object)
def __init__(self, *args, **kwargs):
self.foo = kwargs('foo', None)
self.bar = kwargs('bar', None)
@property
def baz(self):
if self.foo is not None and self.bar is not None
return self.foo + self.bar
else:
return None
@baz.setter
def baz(self, baz):
self.__baz = baz
# i plan using outside also
my_class = MyClass(foo=1,bar=2)
print my_class.baz
# 3
my_class = MyClass(foo=1)
print my_class.baz
# None
It's right having a @property that will only have this composed return value?
Should i use another approach? like __setattr__
?
Edit: The duplicate doesnt answer my question, the class im using has a object inheritance, my question is about if it's the @property getter can return baz value as foo + bar
instead of another approach