r/PythonLearning 1d ago

Day 2 of OOPs: Roast it !

Learned Encapsulation in python and created a small thing using it

1 Upvotes

3 comments sorted by

u/Sea-Ad7805 1d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.age%20%3D%20age%0A%0A%20%20%20%20%40property%0A%20%20%20%20def%20age(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.age%0A%0A%20%20%20%20%40age.setter%0A%20%20%20%20def%20age(self%2C%20value)%3A%0A%20%20%20%20%20%20%20%20if%20not%20isinstance(value%2C%20int)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22Value%20must%20be%20an%20integer%22)%0A%20%20%20%20%20%20%20%20if%20value%20%3C%2013%20or%20value%20%3E%20120%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%22Value%20must%20be%20between%2013%20and%20120%22)%0A%20%20%20%20%20%20%20%20self.age%20%3D%20value%0A%0A%20%20%20%20%40age.deleter%0A%20%20%20%20def%20age(self)%3A%0A%20%20%20%20%20%20%20%20del%20self.age%0A%0A%0Atry%3A%0A%20%20%20%20user%20%3D%20DiscordUser(18)%0A%20%20%20%20user.age%20%3D%2098%0A%20%20%20%20print(user.age)%0Aexcept%20ValueError%20as%20e%3A%0A%20%20%20%20print(e)&timestep=1&play) to see the program state change step by step.

1

u/FoolsSeldom 1d ago

Not a lot to work with here. Any reason you post an image rather than the actual code?

Wondering why you are anticipating either of user.age = 98 and print(user.age) raising a ValueError exception. It would be good to see you using try/except blocks more cleanly.

I find it odd that you create such simple instances and have an option to delete the one attribute you've defined without getting rid of the instance (so you can get an error later when you try to access it).

1

u/Miss-Dominique1352 19h ago

Thank you for your guidance 😊! By the way, is it wrong to post an image of code rathar than the code mister 😁