Re: Using my routines as functions AND methods

Alan Gauld <[email protected]>
Newsgroups comp.lang.python
Message-ID <[email protected]>
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?

-- 
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.