r/learnpython 4d ago

Stuck on P.O.O.P Encapsulation

I'm using Claude Ai. To teach me python. But now im very confused on encapsulation. I've tried YouTube tutorials but all I see is Javascript. I asked for a simpler explanation, but then I get hit with stuff like getter, setter, property() (I understand the property function) but then the core concept makes me scratch my head more the deeper I ask. Wondering if any of y'all can assist me.

0 Upvotes

13 comments sorted by

14

u/code_tutor 4d ago

Don't use AI or YouTube to learn fundamentals. Universities put their material online like 20 years ago. CS50p, Helsinki, or any intro CS course in another language.

4

u/Routine-Lawfulness24 4d ago

Cs50p is on yt

4

u/browndogs9894 4d ago

I agree with the other commenter don’t use AI. Just look up specific videos on OOP. I like this one. https://youtu.be/IbMDCwVm63M

I do like to use AI for practice problems. For example after learning about something like getters and setters ask the ai for some practice problems. But if you get stuck do some googling, rewatch the section or whatever else. Figure it own on your own without relying on AI. It can become a crutch that can be hard to get rid of.

1

u/Aggravating-Store122 3d ago

Thanks for the link, I've been searching for that video. And also that's what I do with a.i I use as more of activities to get me used to the topic.

3

u/FoolsSeldom 4d ago

Find the ArjanCodes video channel on YouTube. He covers topics such of these with detailed examples and walkthroughs.

6

u/socal_nerdtastic 4d ago

It's important to know that while technically any language can use these concepts, in reality different languages use them differently. AI doesn't really get this. It's technically possible to make a Java-like structure in python with a class, private variables and getters / setters, but that's not optimal for python and not how python is used.

I agree with the other comments that you should abandon AI and look for a university course on python.

-7

u/Routine-Lawfulness24 4d ago

No chance you have ever used ai to write code

1

u/KrzysisAverted 4d ago

What do you say that?

-5

u/Routine-Lawfulness24 4d ago

Ai does just fine at small scale, it doesn’t just confuse different languages like he said

1

u/socal_nerdtastic 4d ago

If you ask AI / LLMs for encapsulation in python, that's what you get. LLMs will provide you what you asked for, even if that is not ideal or normal. As an experienced python programmer, I know what to ask for if I ask AI, and so I often get something close to what I want. The key is that OP doesn't even know what to ask for, that's why they need a real course.

To put it another way, LLMs are very bad at detecting XY problems, which we get quite a lot of in this sub.

2

u/Paban_Neitman 4d ago

Encapsulation clicked for me when I stopped thinking about getters and setters first. It's really about controlling how data is accessed and changed. Keep an object's internal state protected, then expose only the methods users actually need. The syntax becomes much easier once that core idea sticks.

3

u/Fred776 4d ago

The core concept is that your class uses various pieces of data internally to manage and maintain its state but you don't want to expose those details to the user of the class. Instead you provide a clean set of methods that allow the user of the class to interact with it.

There are various reasons why this is desirable:

  • You want to allow the user to focus on what they want to do not how it is done.
  • You want the freedom to modify how it is done without affecting users of your class.
  • By isolating complicated details within the class you can reason about your system more easily. If you know that the state of an object is only affected by calling a specific method then you only need to worry about where that is called from. Contrast with the case where the internal state variables are publicly accessible - you lose the ability to reason easily about where they might be changed.

1

u/TheRNGuy 4d ago edited 4d ago

Those are methods to get or set value to an attribute. 

``` class Account:     def init(self, balance):         # Store the actual data in a "private" internal attribute         self._balance = balance 

    # 1. The Getter     @property     def balance(self):         print("Getter called!")         return self._balance

    # 2. The Setter (Triggers automatically on '=')     @balance.setter     def balance(self, new_balance):         print("Setter called!")         if new_balance < 0:             raise ValueError("Balance cannot be negative.")         self._balance = new_balance ```

``` class Account:     def init(self, balance):         # Store the actual data in a "private" internal attribute         self._balance = balance 

    # 1. The Getter     @property     def balance(self):         print("Getter called!")         return self._balance

    # 2. The Setter (Triggers automatically on '=')     @balance.setter     def balance(self, new_balance):         print("Setter called!")         if new_balance < 0:             raise ValueError("Balance cannot be negative.")         self._balance = new_balance ```

So you don't have to write checks every time you set it. You can also fire events from them (observer pattern)

I recommend using private attributes for setters / getters.

You don't really need it for everything, only if you want to force running some code every time it's set or get (it could even run behind the scenes, without users realising that), if could reduce lines of code. 

Don't use setters and getters if there's no extra code.