• Re: Using my routines as functions AND methods

    From Thomas Passin@21:1/5 to Thomas Passin on Thu Jan 4 00:11:16 2024
    On 1/3/2024 11:17 PM, Thomas Passin 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)
    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

    If your requirements are not very tricky, you can write a
    convert-to-method function yourself:

    def f1(x):
    print(x)
    f1('The plain function')

    class Class2:
    pass

    def convert_method(f):
    """Assign existing method without a "self" arg
    as a class's method.
    """
    def fnew(instance, *args):
    f(*args)
    return fnew

    Class2.newfunc = convert_method(f1)
    c2 = Class2()
    c2.newfunc('f1 assigned as method of Class2') # Prints the arg

    This example does not make f1 aware of the self argument, but you asked
    to convert an existing function, and that function would not be aware of
    the self parameter. It's much like a decorator function, but is not here
    being used as a decorator. If you meant something else, please think out
    what you want and explain that.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Thomas Passin on Thu Jan 4 07:04:46 2024
    Thomas Passin <list1@tompassin.net> writes:
    def convert_method(f):

    You guys could also check out "MethodType" from "types".

    from types import *

    def f():
    pass

    f.i = 0

    def inc( f ):
    f.i += 1

    f.inc = MethodType( inc, f )

    f.inc()

    print( f.i )

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Alan Gauld via Python-list on Wed Jan 3 23:17:34 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Thomas Passin via Python-list on Sat Jan 6 17:53:43 2024
    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.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----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 rBQic3JG2Zyy2uwd9WL33BODnZqhspZOBFfQrop