Python 3K or Python 2.9?

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Sep 12 22:20:41 EDT 2007


TheFlyingDutchman <zzbbaadd at aol.com> writes:

> I would mention that an instance is passed as the first parameter
> argument of a method if the methods were declared with the extra
> argument and called with the extra argument:
> 
> a = MyClass()
> 
> my_method(a,someParameter)

Are you unaware that this is the way it works already?

    >>> class Foo(object):
    ...     def bar(self, baz):
    ...         print baz
    ...
    >>> foo = Foo()
    >>> Foo.bar("spam")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: unbound method bar() must be called with Foo instance as first argument (got str instance instead)
    >>> Foo.bar(foo, "spam")
    spam
    >>> foo.bar("spam")
    spam

The latter two statements are equivalent. The 'instance.method(args)'
syntax is just sugar for 'Class.method(instance, args)'.

-- 
 \            "Choose mnemonic identifiers. If you can't remember what |
  `\             mnemonic means, you've got a problem."  -- Larry Wall |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list