Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python getters and setters

  • 23-08-2020 12:32PM
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators, Paid Member Posts: 81,198 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, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


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


  • Moderators, Computer Games Moderators, Social & Fun Moderators, Paid Member Posts: 81,198 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, Registered Users 2 Posts: 6,176 ✭✭✭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, Registered Users 2 Posts: 2,216 ✭✭✭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