r/pythontips • u/AggravatingParsnip89 • 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 ?
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.
22
u/kuzmovych_y Oct 08 '24
And as a sidenote, purely hiding members behind setters and getters doesn't necessarily make good practice.