1

Here is the homework question, I'm just totally unsure of where to go from here: In Python, write a class called Act that contains: an instance field of type int called num_members that denotes the number of members of the act (e.g. the number of musicians in a band), an instance field of type string called name for the act, an instance field of type string called kind for the kind of the act (band, comedian, magician, etc), an instance field of type string called stage for the stage the act will play on, a constructor that takes four arguments, one for each instance field, and sets the fields appropriately, a constructor that takes all arguments except the stage and sets stage to null or Nil (setting all other fields appropriately), getter methods for all fields, a setter method for all fields.

Once you have written this class, we need a nice way of representing it to the user. Add a method to the class Act that overrides the str method. This method should construct a string representation of the class that includes the values of all fields.

Here is what I have written thus far:

class Act:
def __init__(self, num_members, name, kind, stage = None):
    self._num_members = num_members
    self._name = name
    self._kind = kind
    self._stage = stage
def blablamethod():
    
    
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

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

num_members = property(get_num_members, set_num_members)
name = property(get_name, set_name)
kind = property(get_kind, set_kind)
stage = property(get_stage, set_stage)

def__str__(self):
    string = "Number of members:" + str(self.num_members)
    string += "/nName:" + self.name
    string += "/nKind of act:" + self.kind
    if self.stage: 
        string += "/nStage:" + self.stage
     return string
General Grievance
  • 4,555
  • 31
  • 31
  • 45
nil
  • 33
  • 4
  • 1
    Welcome 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 Answers2

2

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
  • 51
  • 2
  • 6
1

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,918
  • 4
  • 21
  • 43