On 1/3/2024 8:00 PM, Alan Gauld via Python-list wrote:
On 03/01/2024 22:47, Guenther Sohler via Python-list wrote:
Hi,
In my cpython i have written quite some functions to modify "objects".
and their python syntax is e.g.\
translate(obj, vec). e.g whereas obj is ALWAYS first argument.
However, I also want to use these functions as class methods without
having
to
write the function , twice. When using the SAME function as a methos,
the
args tuple must insert/contain "self" in the first location, so i have
written a function to do that:
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
Then you can call it as
myObject = MyClass()
myObject.myMethod()
A naive example seems to work but I haven't tried anything
complex so there is probably a catch. But sometimes the simple
things just work?
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
def convert_method(f):
On 03/01/2024 22:47, Guenther Sohler via Python-list wrote:
Hi,
In my cpython i have written quite some functions to modify "objects".
and their python syntax is e.g.\
translate(obj, vec). e.g whereas obj is ALWAYS first argument.
However, I also want to use these functions as class methods without having >> to
write the function , twice. When using the SAME function as a methos, the
args tuple must insert/contain "self" in the first location, so i have
written a function to do that:
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
Then you can call it as
myObject = MyClass()
myObject.myMethod()
A naive example seems to work but I haven't tried anything
complex so there is probably a catch. But sometimes the simple
things just work?
On 1/3/2024 8:00 PM, Alan Gauld via Python-list wrote:^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
On 03/01/2024 22:47, Guenther Sohler via Python-list wrote:
Hi,
In my cpython i have written quite some functions to modify "objects". and their python syntax is e.g.\
translate(obj, vec). e.g whereas obj is ALWAYS first argument.
^^^However, I also want to use these functions as class methods without having
to
write the function , twice. When using the SAME function as a methos, the args tuple must insert/contain "self" in the first location, so i have written a function to do that:
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
Then you can call it as
myObject = MyClass()
myObject.myMethod()
A naive example seems to work but I haven't tried anything
complex so there is probably a catch. But sometimes the simple
things just work?
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 66:47:34 |
Calls: | 6,712 |
Files: | 12,244 |
Messages: | 5,356,315 |