0

I've worked as software engineer for some years, but python beginner. So I searched python codes in Github to study how to write codes. Then, I couldn't find out the code which double underscore is used as private instance variables. I wonder why it is. Because you know, if you've used Java, it's uncomfortable that there are a lot of public instance variables in code. Are there explicit reasons?

Actually I understand how the double underscore behavior in codes. The point I want to say is why almost python users don't use double underscore to make instance variables private.

For example
https://github.com/google/google-api-python-client/blob/master/googleapiclient/channel.py

self.type = type
self.id = id
self.token = token
self.address = address
self.expiration = expiration
self.params = params
self.resource_id = resource_id
self.resource_uri = resource_uri

Although Google decide to use private instance variables with double underscore in their coding guide, they don't use it.

https://google.github.io/styleguide/pyguide.html#Naming

Instance Variables|lower_with_under|_lower_with_under (protected) or __lower_with_under (private)
ykensuke9
  • 714
  • 2
  • 7
  • 15
  • Double-underscore names are just as "public" as single-underscore names. There's just a bit of usually-unnecessary name mangling to avoid attribute name collisions. – user2357112 Dec 25 '15 at 10:25
  • Also, things like `property` mean there's no reason to stick attributes behind `get_thing` and `set_thing` methods. If you actually need to add logic to getting/setting an attribute, you can. – user2357112 Dec 25 '15 at 10:28

0 Answers0