On 04/01/2024 04:17, Thomas Passin via Python-list wrote: >> I'm probably missing something obvious here but can't you >> just assign your function to a class member? >> >> def myFunction(obj, ...): ... >> >> class MyClass: >> myMethod = myFunction > > That works if you assign the function to a class instance, but not if > you assign it to a class. > > def f1(x): > print(x) > f1('The plain function') > > class Class1: > pass > > class Class2: > pass > > c1 = Class1() > c1.newfunc = f1 > c1.newfunc('f1 assigned to instance') # Works as intended > > Class2.newfunc = f1 > c2 = Class2() > c2.newfunc('f1 assigned to class') # Complains about extra argument
Yes but I did the assignment inside the class definition and that seemed to work just fine: >>> def g(obj, st): print(st, obj.v) ... >>> class D: ... def __init__(self,v): self.v = v ... m = g ... >>> d = D(66) >>> g(d,'val = ') val = 66 >>> d.m('v = ') v = 66 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list