Re: Using my routines as functions AND methods

"Peter J. Holzer" <[email protected]>
Newsgroups comp.lang.python
Message-ID <[email protected]>
On 2024-01-03 23:17:34 -0500, Thomas Passin via Python-list wrote:
> 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)

You omitted the first argument (obj).

That should be 

def f1(obj, x):
    print(x)


> f1('The plain function')

> 
> class Class1:
>     pass

o = Class1()
f1(o, 'The plain function')

works for me.


> class Class2:
>     pass
> 
> c1 = Class1()
> c1.newfunc = f1
> c1.newfunc('f1 assigned to instance') # Works as intended

Now this doesn't work any more (but the OP doesn't want that anyway,
AFAICT).


> Class2.newfunc = f1
> c2 = Class2()
> c2.newfunc('f1 assigned to class')  # Complains about extra argument

But this does.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email protected]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmWZhZIACgkQ8g5IURL+
KF0bgA//Znc12xLSsBah4lZNWaXrLtKqJbeu8J4MaHxqiq7ou1Pg2FCIn5zUUZlA
73p+Ce12N/KA0nX280JhwgnIp9N9XK4JGYwybW1vikR5B+rLhXxgmeT4bmo6Dlh/
ycCu2hzPZZ55UPK1FDAmA/g0TEjMawuahq4otKJNjvb1iEAOHiNEoRepH4cxnLFU
GxPqCrbBDJ2FCYIi/uuY8BLc+ZwzPjr/NLBa40usB3dzWyNd1KoGwDAgSqJMrsXx
u18oQ7BJQcDLrABI82cU0LkNSB3ZZewgZtmivJqHBNMt8wnJcT0m2b34syn4PYzd
/4W7Mb05qw8ZYz8vFzVrwqhriYEbzMuCu09d5qOAlwzpwiN8bBZpYLn4S5C+Mszd
BuTIlRhgq60D0KFRwqW1UVOJUhb8iMWd3SByGaSCqT7gOZyj069eBZPFFaPaFtvY
XakEXmzjf72908d4SPV6s/mNKC2HwVW4XM+3jaW9/xUBd8GCfUYRFDDz3XEro9SL
Rlgt9MbDxnv/fC3ilhuvTIRQdixXJPBQK4AE4CwfyTKyPveAe+dyL9u8+nYrpl1s
Ls6MPt45Ly+huNwqPSyxerHHO0EkbYxloCwXsofD3maYhg7PV7GCSTIPz1PPlZQT
rBQic3JG2Zyy2uwd9WL33BODnZqhspZOBFfQropEbSUQgISU2GI=
=TD8Z
-----END PGP SIGNATURE-----
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.