0

"The class should initialize with a name and a birthday, but the birthday should be None." "There should be two methods, name and birthday" "setBirthday sets their Birthday to a date" these are the instructions I was given, this is a small piece in the larger picture.

I'm trying to set the day, the month and year to be entered in by the user...this will be used later for other calculations..

Code:

class person(object):
    def __init__(self, name):
        self.name = name
        self.setBirthday = None

#getName returns the name of the person         
    def getName(self):
        return self.name

#setBirthday sets their Birthday to a date
    def setBirthday(self):
        day = (raw_input('Please enter the date of the month you were born on here ->'))
        return self.day
        month = (raw_input('Please enter the month of the year you were born on here ->'))
        return self.month
        year = (raw_input('Please enter the year you were born on here ->'))
        return self.year        

varName = person(raw_input('Please enter your name here ->'))
varDate = person.setBirthday()

Can you point me in the right direction? This class stuff really confuses me...I need to have the user be able to input the day, month and year to be saved for later use. error is below in comments. I did remove the returns in my code.

Staley
  • 55
  • 2
  • 11
  • 1
    What is missing in your code? Please ask a concrete question. –  Feb 23 '16 at 07:45
  • The problem here isn't classes at all; you can only return once from any function, but your `setBirthday` method is trying to return three times. – Daniel Roseman Feb 23 '16 at 07:47
  • The first return stops the execution of the function setBirthday, so setting the month and year is never executed. You probably don't have to return anything in that function. – mwil.me Feb 23 '16 at 07:47
  • "`setBirthday`"... Your professor is teaching you to write Python programs in Java, and it is doing you no favors. You've got issues with masking, code that will never execute, and so on. – TigerhawkT3 Feb 23 '16 at 07:49
  • so here is the error...varDate = person.setBirthday() TypeError: unbound method setBirthday() must be called with person instance as first argument (got nothing instead) – Staley Feb 23 '16 at 07:51
  • Among various other problems, when you create a `person` instance `self.setBirthday = None` replaces the `setBirthday` with `None`, which will prevent you from calling the method. – PM 2Ring Feb 23 '16 at 07:55

1 Answers1

2
  1. Your class defines setBirthday as a method, but as soon as you instantiate the class that method disappears and setBirthday just points to None. Note that birthday and setBirthday are two unique names, and that class is already using the latter for that method.
  2. You have unnecessary parentheses. Try to use only what is necessary for grouping, with the occasional extra pair when required for clarity. var = (input()) is not such an occasion.
  3. Python does not require getters and setters. There's no such thing as a private variable, so anyone who wants to modify something can do so to their heart's content. The only thing I'm aware of that can't be redefined is a property (a method tagged with the @property decorator).
  4. You are confusing instance variables with global variables, and when to turn each into the other. The setters in that class should simply set instance variables, not return them to the caller (it's like this in Java as well, which is where you actually would use getters and setters.
  5. When you call a class's instance methods directly, such as MyClass.methodname(), you need to pass it an instance of the class. This is why standard Python style (PEP-8) calls for class names to be Uppercase or UpperCase and other objects to be lowercase or snake_case. Naming the class person made it look like an instance of something when it's actually a class, making it all too easy to misuse it.

Here is what the code should look like:

class Person(object):
    def __init__(self, name):
        self.name = name
        self.birthday = None

    def getName(self):
        '''return the name of the person'''
        # this method should be removed. to get a person's name,
        # use "person.name" instead of "person.getName()"
        return self.name

    def setBirthday(self):
        '''set their Birthday to a date'''
        day = raw_input('Please enter the date of the month you were born on here ->')
        month = raw_input('Please enter the month of the year you were born on here ->')
        year = raw_input('Please enter the year you were born on here ->')
        self.birthday = int(day), int(month), int(year)

person = Person(raw_input('Please enter your name here ->'))
person.setBirthday()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Ok that makes more sense now, like I said these class problems are hard. Definitely apppreciate your feedback. So next step is to use this info to determine how many days old the individual is. Then add in if statements for not entering in correct data for instance an integer for the day. – Staley Feb 23 '16 at 08:30
  • See [here](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) for input validation and [here](https://docs.python.org/3/library/datetime.html) for doing calculations with dates. – TigerhawkT3 Feb 23 '16 at 08:35