Possible Duplicate:
default value of parameter as result of instance method
While it is possible to set default values to function parameters in python:
def my_function(param_one='default')
...
It seems not to be possible to access the current instance (self):
class MyClass(..):
def my_function(self, param_one=self.one_of_the_vars):
...
My question(s):
- Is this true that I cannot access the current instance to set the default parameter in functions?
- If it is not possble: what are the reasons and is it imaginable that this will be possible in future versions of python?
There is much more to it than you think. Consider the defaults to be static (=constant reference pointing to one object) and stored somewhere in the definition; evaluated at method definition time; as part of the class, not the instance. As they are constant, they cannot depend on
self
.Here is an example. It is counterintuitive, but actually makes perfect sense:
This will print
1 2 1 1 3
.Because it works the same way as
Obviously, if you modify
default_s
, it retains these modifications.There are various workarounds, including
or you could do this:
Then the method
makeDefaultS
will have access toself
.Another variation:
here the default value of
s
is a factory function.You can combine all these techniques:
It's written as:
And I think it's safe to say that will never happen in Python due to the nature that
self
doesn't really exist until the function starts... (you can't reference it, in its own definition - like everything else)For example: you can't do
d = {'x': 3, 'y': d['x'] * 5}
Default value for parameters are evaluated at "compilation", once. So obviously you can't access
self
. The classic example islist
as default parameter. If you add elements into it, the default value for the parameter changes!The workaround is to use another default parameter, typically
None
, and then check and update the variable.There are multiple false assumptions you're making here - First, function belong to a class and not to an instance, meaning the actual function involved is the same for any two instances of a class. Second, default parameters are evaluated at compile time and are constant (as in, a constant object reference - if the parameter is a mutable object you can change it). Thus you cannot access
self
in a default parameter and will never be able to.