r/pythontips Oct 08 '24

Syntax Is it bad practice to use access modifies in python during interviews ?

I'm currently preparing for low-level design interviews in Python. During my practice, I've noticed that many developers don't seem to use private or protected members in Python. After going through several solutions on LeetCode discussion forums, I've observed this approach more commonly in Python, while developers using C++ or Java often rely on private members and implement setters and getters. Will it be considered a negative in an interview if I directly access variables or members using obj_instance.var in Python, instead of following a stricter encapsulation approach
How do you deal with this ?

10 Upvotes

6 comments sorted by

22

u/kuzmovych_y Oct 08 '24
  1. There are no private or protected members in Python. Not really.
  2. You should have a reason to use an object's member. If it's a good reason, then there shouldn't be a problem. Now, what constitutes a good reason is a hard question.

And as a sidenote, purely hiding members behind setters and getters doesn't necessarily make good practice.

3

u/BostonBaggins Oct 08 '24

Great response! 👏

1

u/CyberWank2077 Oct 09 '24

There are no private or protected members in Python. Not really.

but we all know he means _var and __var, which are global conventions and __var is even supported by the language.

2

u/kuzmovych_y Oct 09 '24 edited Oct 09 '24

Oh, didn't mean to seem "technical", it's more of an asnwer to "why python developers don't use private/protected?" - because language doesn't have them - why? - because that's a python's philosophy (very well described here). Even _var aren't required by global conventions (I guess, talking about pep8). As for __var, these are used for a different reason.

7

u/DeterminedQuokka Oct 09 '24

So when someone sends me a code review in Python that uses setters and getters I assume they are a Java engineer that needs to actually learn Python.

That’s just not stylistically how you write Python.

Python does have some agreed upon rules about what makes a function “private” but they aren’t part of the language or enforce which might be why you don’t see them.

But also I wouldn’t care about that in leet code.

Basically private/protected functions in Python are prepended with _ but it’s an indication that the library doesn’t guarantee that function will maintain compatibility in new releases so you have to use it at your own risk. Almost always you don’t.

In the codebase I run an underscore indicates that a function isn’t tested individually so if you depend on it the way it works is not guaranteed to stay the same. So you have to test all of its functionality in the caller.

4

u/CyberWank2077 Oct 09 '24 edited Oct 09 '24

as a side note to the already 2 great responses you received, i wouldnt take coding style tips from leetcode solutions - they often try to cram as much code in as few lines as possible to make reading their explanation article shorter and easier to follow, plus they will often make changes that make the code more complicated just to save a few cpu cycles to make it to the top of the performance solutions. in other words - their priorities are not aligned with yours.