Confused about bound functions

Levente Sandor nospam at newsranger.com
Sat Aug 18 13:19:16 EDT 2001


In article <20xf7.12187$P15.6890321 at news1.rdc1.sfba.home.com>, Theodore D.
Sternberg says...
>
>Why do I need to pass in a self argument, in the
>last line of the following program?
>
>------------------------
>class C:
>    pass
>
>c = C()
>cmd = 'def foo(self): print "I am foo"'
>exec cmd in c.__dict__
>
>c.foo(c)
>------------------------
>
>I'd like to be able to say simply "c.foo()".
>How can I arrange for that?
>
>Theodore Sternberg
>Fremont, Cal. USA


Your code adds the foo method to the c instance of the C class.
Change def foo(self) to def foo()
OR
c.__dict__ to C.__dict__
In the second case the method will be added directly to the class.
Levi










More information about the Python-list mailing list