done here, thank you! See answer in comments
Asked
Active
Viewed 145 times
0
-
1Welcome to SO! Thanks for showing what you have attempted and where you are stuck. Even so, you should refer to this: https://stackoverflow.com/help/minimal-reproducible-example to format your question better. – Harshal Parekh Dec 01 '19 at 20:30
-
This style of getters and setters is not pythonic. – AMC Dec 02 '19 at 01:11
2 Answers
1
Your code already meets the requirements: Act can be instantiated with 4 arguments, or with 3 arguments, in which case the stage is set to None. This use of optional parameters is good practice in python, but if you want to you can add a completely separate constructor implemented as a @classmethod as well:
class Act:
def __init__(self, num_members, name, kind, stage):
self._num_members = num_members
self._name = name
self._kind = kind
self._stage = stage
@classmethod
def without_stage(cls, num_members, name, kind):
return cls(num_members, name, kind, None)
Now you can instantiate Act as Act(num_members, name, kind, stage) or as Act.without_stage(num_members, name, kind).
Aran-Fey
- 41
- 2
- 5
0
Read more about having multiple constructors here: What is a clean, pythonic way to have multiple constructors in Python?
I was able to fix some issues in your code, please find them mentioned in my comments:
class Act:
# works fine
def __init__(self, num_members, name, kind, stage=None):
self._num_numbers = num_members
self._name = name
self._kind = kind
self._stage = stage
# all the getters work fine
def get_num_members(self):
return self._num_numbers
def get_name(self):
return self._name
def get_kind(self):
return self._kind
def get_stage(self):
return self._stage
# all the setters work fine now
def set_num_members(self, num_numbers):
self._num_numbers = num_numbers
def set_name(self, name):
self._name = name
def set_kind(self, kind):
self._kind = kind
def set_stage(self, stage):
self._stage = stage
# I did not see the need for properties
# def__str__(self): your code did not have a space here
# ^ here
# "\n" is newline and not "/n"
def __str__(self):
string = "Number of members:" + str(self._num_numbers)
string += "\nName: " + self._name
string += "\nKind of act: " + self._kind
if self._stage:
string += "\nStage: " + self._stage
return string
# create the object
a = Act("5", "h", "6", "7")
print(a)
Hope this helps. Good luck.
Harshal Parekh
- 5,381
- 4
- 16
- 39
-
-
I meant that for the constructors only which is mentioned in the linked answer. – Harshal Parekh Dec 02 '19 at 01:54
-