Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an attribute of the current class instance as a default value for method's parameter [duplicate]

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?
like image 294
paweloque Avatar asked Nov 02 '12 13:11

paweloque


2 Answers

It's written as:

def my_function(self, param_one=None): # Or custom sentinel if None is vaild     if param_one is None:         param_one = self.one_of_the_vars 

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}

like image 187
Jon Clements Avatar answered Oct 06 '22 00:10

Jon Clements


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:

def add(item, s=[]):     s.append(item)     print len(s)  add(1)     # 1 add(1)     # 2 add(1, []) # 1 add(1, []) # 1 add(1)     # 3 

This will print 1 2 1 1 3.

Because it works the same way as

default_s=[] def add(item, s=default_s):     s.append(item) 

Obviously, if you modify default_s, it retains these modifications.

There are various workarounds, including

def add(item, s=None):     if not s: s = []     s.append(item) 

or you could do this:

def add(self, item, s=None):     if not s: s = self.makeDefaultS()     s.append(item) 

Then the method makeDefaultS will have access to self.

Another variation:

import types def add(item, s=lambda self:[]):     if isinstance(s, types.FunctionType): s = s("example")     s.append(item) 

here the default value of s is a factory function.

You can combine all these techniques:

class Foo:     import types     def add(self, item, s=Foo.defaultFactory):         if isinstance(s, types.FunctionType): s = s(self)         s.append(item)      def defaultFactory(self):         """ Can be overridden in a subclass, too!"""         return [] 
like image 44
Has QUIT--Anony-Mousse Avatar answered Oct 05 '22 23:10

Has QUIT--Anony-Mousse