Set run vars with each call

Ian Kelly ian.g.kelly at
Tue Jul 12 20:59:18 CEST 2011

On Tue, Jul 12, 2011 at 11:50 AM, Andrew Berg <bahamutzero8825 at> wrote:
> On 2011.07.12 12:32 PM, Gnarlodious wrote:
>> OK, that sets a value at init time. But is there a similar built-in
>> to run whenever the class instance is called?
> What do you mean by call an instance? Do you want to run certain code
> whenever any method is called? Do you want to want certain code to run
> whenever an attribute is accessed? Calling an instance doesn't make any
> sense, especially if you're not referring to the __init__() method.

If I'm understanding correctly, I think the OP wants to do something like this:

class Gadget:
    def do_something(self, some_argument=some_default_value):
        # do stuff

where the exact default value of some_argument depends on the current
state of the Gadget instance.  The canonical approach here would be:

class Gadget:
    def do_something(self, some_argument=None):
        if some_argument is None:
            some_argument = self._some_argument_default
        # do stuff

And then the other instance methods of Gadget can update the default
by setting the value of the _some_argument_default attribute.

More information about the Python-list mailing list