DEV Community

Discussion on: Learning Python: From Zero to Hero

Collapse
 
sfarkas1988 profile image
Sandor Farkas

Thank you for your post. It’s nothing new to me but reminds me on continuing my first python project.

It’s written nicely but the encapsulation stuff is a bit confusing. The underscore as a prefix is really only a convention, in real it still behaves like a public attribute/method?
I think it can confuse newbies a bit.

And as I saw it already many times, writing variables and methods in snake_case seems to be the python standard, right?

Have fun!
Best
Sandor

Collapse
 
teekay profile image
TK

Hey Sandor, thanks for the feedback. I tried my best to mention that the "underscore" is just a convention using the PEP8

We don’t use the term “private” here, since no attribute is really private in Python (without a generally unnecessary amount of work). — PEP 8

...and Python Software Foundation documentation:

“‘Private’ instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member)” — Python Software Foundation

As I mention in this part

We can access and update it. Non-public variables are just a convention and should be treated as a non-public part of the API.

...so yes, it can be accessed, but as a convention we treat it as a non-public part of the API.

Which part was confusing? Let me know how I can update it to remove this complexity.


And yes, Python is a "snake_case programming language" :)

Thanks again for the feedback!

Collapse
 
sfarkas1988 profile image
Sandor Farkas

You really explained it great, I personally think it’s hard to understand for someone who worked with i.e. java that accessibility is just a convention.
I wouldn’t have posted the code example on how to access private properties and functions, because with this you demonstrate the opposite purpose.