3

In Python, which is the best way (style wise) to allow public access to an object's variables?

There are lots of options I've seen from different languages, I was wondering which of these (if any) is the preferred Python method? These are the options I'm currently torn between:

  1. Allow direct access to object variables (e.g. print(object.variable)), ignore data hiding
  2. Allow access to object variables by a wrapper function:

class X:
    variable_a = 0
    variable_b = 0
...
    def get_variable_a(self):
        return self.variable_a

If this is the recommended way, how do you name the methods? (get_variablename(), or just variablename etc?)

What does everyone recommend for this?

thanks!

Lucy

2
  • I think the answer, as always, is going to be "it depends." Commented Feb 20, 2010 at 19:41
  • There are persuasive arguments for using properties instead of getters/setters here: stackoverflow.com/questions/1554546/… Commented Feb 20, 2010 at 20:04

4 Answers 4

4

Don't bother using accessors until they're necessary; converting a simple attribute to a property is quick and easy, and doesn't need modification of client code.

When I write a property, I use _get_FOO() and _set_FOO() for the accessors, and _FOO for the attribute itself.

Sign up to request clarification or add additional context in comments.

2 Comments

@Ingacio, do you really use a leading underscore for properties which you intend to access from outside the class/instance? That goes against a strong Python convention, where _foo says "not public!" Or was the underscore in _FOO just a typo?
@Peter: The property is called FOO. The store is called _FOO.
3

There are no "private" variables in Python. Check section 9.6 of this document http://docs.python.org/tutorial/classes.html

There is a convention though, that the users of the class should treat variables with names, starting with underscore to be not touched directly. Still, there is no mechanism which may prevent this.

Same with the mangled names (starting with 2 underscores).

Comments

2

Just allow direct access to any variables that you wish to expose as the public API. If you need to change the behavior you can always turn it into a property later on.

Notice that this is orthogonal to information hiding. obj.get_some_implementation_detail() is no better than obj.some_implementation_detail.

Comments

-1

You can use

Getters and setters (Java like)

class SomeClass(object):
  ...

  def get_x(self):
    return self._x
  def set_x(self, x):
    self._x = x

c = SomeClass()
print c.get_x()
c.set_x(10)

Properties (C# like)

class SomeClass(object):
  ...

  def get_x(self):
    return self._x
  def set_x(self, x):
    self._x = x
  x = property(get_x, set_x)

c = SomeClass()
print c.x
c.x = 10

I think it is merely a matter of style. Choose the one you like better. Same applies to the naming convention, choose one convention and stick to it.

In any case, data hiding can be done with pseudo-private variables (beginning with two underscores). They cannot be accessed directly, as opposed to the example variables (_x not starting with two underscores).

8 Comments

Actually - they can. Just with a different name, which includes the class name. The name of the variable is mangled, not its access level. From the same document you refer: "Note that the mangling rules are designed mostly to avoid accidents; it still is possible to access or modify a variable that is considered private."
"“Private” instance variables that cannot be accessed except from inside an object, don’t exist in Python." (from the page you linked to)
You can use getters and setters, but Python has an entire mechanism to save you having to deal with them. Protection is built in. See "Accessors: Why Python Doesn’t Have Them":albert.infinitepigeons.org/wp/?p=135
Why the downvotes? I clearly wrote "cannot be accessed directly" which is completely true because with a instance variable "__x" you cannot access "instance.__x" but must use "instance._ClassName__x". It's about how you use such variables - as long as you don't access any variables called .__something (and instead use the property .something), you're good. Python doesn't enforce it but it should be a convention for every programmer.
You are making mistake to compare variable and its name. __x is just the name, not the variable itself. And the variable is accessible directly (just with another name), i.e. in Pyton you can not forbid this. While properties (or setters and getters) are a way to access a variable indirectly. In the languages, where "private" has meaning, one can not access "private" variables directly. You still can use some form of reflection, but it's not part of the language, but part of the framework, in which this language is used (Java, C#, etc., may use reflection trough framework functions).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.