Simulating call of instance like a list or tuple

Jason Orendorff jason at jorendorff.com
Sat Jan 12 09:35:13 EST 2002


> With Python 2.2's wonderful ability of subclassing built-ins, I began to
> wonder if there was a way to call an instance like a list or tuple, but
> without subclassing them.
>
> In other words, have a class named foo with an instance called bar.  Now,
> call bar like:
> >>> bar
> and have  it return something, just like how calling a list or  tuple like
> that returned the list or  tuple value.

Overload __str__.

class X:
    def __init__(self, name='Joe'):
        self.name = name
    def __str__(self):
        return self.name

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list