8

is there a way to keep a private class variable within the class and still use it as the default value to a non-default variable (without defining that value before, outside of the class)?

example:

class a:
    def __init__(self):
        self.__variable = 6
    def b(self, value = self.__variable):
        print value
2
  • 5
    Python does not have private class variables. At all. The double underscore you are using does not make the variable private, it just does name mangling to avoid name clashes. Don't use double underscores unless you know why you have to. ;) Commented Jan 9, 2011 at 8:03
  • Great question! (Well, it solved a huge problem I had, so...) Commented Jun 8, 2011 at 10:59

3 Answers 3

14

You're overthinking the problem:

class a:

    def __init__(self):
        self.__variable = 6

    def b(self, value=None):
        if value is None:
            value = self.__variable
        print value
Sign up to request clarification or add additional context in comments.

Comments

4
  1. self.__variable is an instance variable, not a class variable.
  2. Because default arguments are evaluated at functionmethod definition time, you can't use an instance variable as default argument - at least not directly (the usual trick, defaulting to None and adding if arg is None: arg = real_default to the function body, does work).
  3. Leading double underscores don't mean "private", they mean "name mangling and asking for trouble". If you don't know exactly what I mean, you shouldn't be using this "feature" at all. The intended use is not private members - use one underscore for that.

5 Comments

What, then, is the intended use?
@Karl: Preventing name clashes in subclasses. See e.g. docs.python.org/reference/… or docs.python.org/tutorial/… (third paragraph).
i can still access the variables if i use only 1 subscore
@calccrypto: Yes, of course. Why not? Actually, the reverse is true: you can't - sanely - access __variable anywhere outside the definition of the class (due to name mangling). That's why I highly suggest you drop one underscore.
@calccrypto yes, that's fine. Python is built on the assumption that we're all adults who understand that accessing another class' variables, without explicit permission (from that class' documentation), is naughty. (And even then, instead of just granting permission, we have property wrappers now.) If I wanted that level of compile-time checking, I wouldn't be using a dynamically-typed language.
4

It's worth adding to this that you can create a custom sentinal value that doesn't get used for any other purpose in your code if you want None to be in the range of allowable arguments. Without doing this, value, None can never be returned from b.

UseDefault = object()

# Python 3? Otherwise, you should inherit from object unless you explicitly
# know otherwise.
class a:    
    def __init__(self):
        self._variable = 6

    def b(self, value=UseDefault):
        if value is UseDefault:
            value = self._variable
        print value

Now None can be passed to b without causing the default to be used.`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.