Is abstract class field in python possible?

When creating a Python class, is it a best practice to use one underscore or two when declaring class variables?

  • class Example(object): __myClassVar = "A Class Var" or ... _myClassVar = "..." This is a question re:convention, the most used one by programmers.  Being Python already creates special methods with double underscores (e.g. __init__, __str__, etc.) I'd like to know if it's a best practice to use a single underscore to indicate a user defined class variable that maintains class name mangling.

  • Answer:

    Short Answer : To use these attributes as private variables, the only way is to make their names start with two underscores and not letting their names end with two underscores. Long Answer : As a matter of fact, two underscores and one underscore is not the same at all. Nor is it a question of convention. A member or even method for that matter, when started with two underscores, will undergo name mangling. Exception being members and methods which both start and end with two underscores. As a rule of thumb : __attr_or_method_name : Can be used as private variables and methods. __attr_or_method_name__ : Generally used to implement special purpsose methods and it doesn't really make sense to come up with our own. _attr_or_method_name : just like any other attribute or method. The starting underscore doesn't make any difference whatsoever. class Example (object) : __private_var = "I am private" _just_another_var = "Nothing different" __special_purpose_var__ = "I don't know why I am special purpose" def __privateMethod (self) : pass def _yetAnotherMethod (self) : pass def __specialPurposeMothod__ (self) : pass example = Example () for attr in dir (example) : print attr And the relevant part, from what it prints, is this... _Example__privateMethod _Example__private_var __specialPurposeMothod__ __special_purpose_var__ _just_another_var _yetAnotherMethod As you can see, name mangling happens only when it starts with two underscores, but doesn't end with two underscores.

Sujeet Gholap at Quora Visit the source

Was this solution helpful to you?

Other answers

I think you need this... http://docs.python.org/tutorial/classes.html#private-variables-and-class-local-references It is more of a convention as there is no concept of private and public methods and attributes in class. Hence "_" is prefixed before private variables. Below is the discussion about private variables, I think its related. http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes

Neeraj Khandelwal

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.