Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python getters and setters

Options
  • 23-08-2020 12:32pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,032 Mod ✭✭✭✭


    Learning about oop in python, bit confused about how getters and setters work, could someone please look at this code and tell me why its not running?
    class Person2:
    
        def __init__(self,name,age):
            self.set_name(name)
            self.set_age(age)
    
        def get_name(self):
            return self.__name
    
        def set_name(self,name):
            self.__name = name
    
        def get_age(self):
            return self.__age
    
        def set_age(self,value):
            if value < 18:
                raise ValueError ("AGE INVALID")
            self.__age = value
    
        def __str__(self):
            return "Name: " + __name + "\nAge: " + str(age)
    
    p = Person2("Tom",17)
    print(p)
    


Comments

  • Registered Users Posts: 2,018 ✭✭✭Colonel Panic


    You're setting age to 17 so it's raising a value error.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,032 Mod ✭✭✭✭Sephiroth_dude


    You're setting age to 17 so it's raising a value error.

    Even when its greater than 18 it fails

    Message=name '_Person2__name' is not defined
    Source=C:\Users\Sephiroth\source\repos\Person2\Person2\Person2.py
    StackTrace:
    File "C:\Users\Sephiroth\source\repos\Person2\Person2\Person2.py", line 22, in __str__
    return "Name: " + __name + "\nAge: " + str(age)
    File "C:\Users\Sephiroth\source\repos\Person2\Person2\Person2.py", line 25, in <module>
    print(p)


  • Registered Users Posts: 6,236 ✭✭✭Idleater


    Even when its greater than 18 it fails

    Did you read the reason why it fails?
    Message=name '_Person2__name' is not defined
    

    Can you see in your code where something is trying to reference the
    __name
    
    variable?

    If not, can you see anywhere in your code where you are trying to reference something that should be
    __name
    
    ?

    When you have fixed this, you have a similar issue with age.


    The <18 check is working fine (or as programmed) for me when I have fixed the above errors and run with
    p = Person2("Tim",18)
    print(p)
    p = Person2("Tom",17)
    print(p)
    


    I get:
    Name: Tim
    Age: 18
    Traceback (most recent call last):
      File "/Users/me/Downloads/Person2.py", line 26, in <module>
        p = Person2("Tom",17)
      File "/Users/me/Downloads/Person2.py", line 5, in __init__
        self.set_age(age)
      File "/Users/me/Downloads/Person2.py", line 18, in set_age
        raise ValueError ("AGE INVALID")
    ValueError: AGE INVALID
    [Finished in 0.0s with exit code 1]
    


  • Registered Users Posts: 1,183 ✭✭✭Neowise


    class Person2:
    
        def __init__(self,name,age):
            self.set_name(name)
            self.set_age(age)
    
        def get_name(self):
            return self.__name
    
        def set_name(self,name):
            self.__name = name
    
        def get_age(self):
            return self.__age
    
        def set_age(self,value):
            if value < 18:
                raise ValueError ("AGE INVALID")
            self.__age = value
    
        def __str__(self):
            return "Name: " + self.__name + "\nAge: " + str(self.__age)
    
    p = Person2("Tom",19)
    print(p)
    
    How about these mods?


Advertisement