• Difference method vs attribut = function

    From Ulrich Goebel@21:1/5 to All on Fri Jun 28 18:08:54 2024
    Hi,

    a class can have methods, and it can have attributes, which can hold a function. Both is well known, of course.

    My question: Is there any difference?

    The code snipped shows that both do what they should do. But __dict__ includes just the method, while dir detects the method and the attribute holding a function. My be that is the only difference?


    class MyClass:
    def __init__(self):
    functionAttribute = None

    def method(self):
    print("I'm a method")

    def function():
    print("I'm a function passed to an attribute")

    mc = MyClass()
    mc.functionAttribute = function

    mc.method()
    mc.functionAttribute()

    print('Dict: ', mc.__dict__) # shows functionAttribute but not method print('Dir: ', dir(mc)) # shows both functionAttribute and method


    By the way: in my usecase I want to pass different functions to different instances of MyClass. It is in the context of a database app where I build Getters for database data and pass one Getter per instance.

    Thanks for hints
    Ulrich


    --
    Ulrich Goebel <ml@fam-goebel.de>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Ulrich Goebel via Python-list on Sat Jun 29 21:53:05 2024
    On 2024-06-28 18:08:54 +0200, Ulrich Goebel via Python-list wrote:
    a class can have methods, and it can have attributes, which can hold a function. Both is well known, of course.

    My question: Is there any difference?

    The code snipped shows that both do what they should do. But __dict__ includes just the method,

    The other way around: It includes only the attributes, not the methods.

    while dir detects the method and the
    attribute holding a function. My be that is the only difference?


    class MyClass:
    def __init__(self):
    functionAttribute = None

    def method(self):
    print("I'm a method")

    def function():
    print("I'm a function passed to an attribute")

    Here is the other main difference: The object is not passed implicitely
    to the function. You have no way to access mc here.

    You can create a method on the fly with types.MethodType:

    import types
    mc.functionAttribute = types.MethodType(function, mc)


    By the way: in my usecase I want to pass different functions to
    different instances of MyClass. It is in the context of a database app
    where I build Getters for database data and pass one Getter per
    instance.

    Or in this case, since each function is specific to one instance, you
    could just use a closure to capture the object. But that might be
    confusing to any future maintainers (e.g. yourself in 6 months), if the
    method doesn't actually behave like a method.

    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+KF0FAmaAZhgACgkQ8g5IURL+ KF1xXw//dkGR4zGztKyqgcQmdBjKjGHKI3V5GoPAzVIOsNYINwN+Fi5od678MYdX d3likfjZOOlJrJHI3NWL8uIU3yBbKfeB5yjXVH0UbYmzYixlkl8x9xhujtSGbTLm uQgV8FlVs4qQfo2ZZhmlRpSRQq4wdRbyQFDDwENm/BflEj64Efx+PbVi11xQgMg4 sTLWU84+xc2aVHs2vv6k8hzrZCUEM67m6uR9ftNZ/DNgCel6qEXs8sTDZ+t3/whj 6X6w3Ve1k0Pp41wdlU8Qy8YZ7jBtRcV703ardrMZx1O8dsWpsl7VPgxpZ+XO7zC0 IftF7b6xmaMiTsJ135of+AUwT0FpfTp23FrHe4rGGqBModru/MuU1cBA4JORAgQq LwRfrfWerUKRGKd04PoAEUFpmPcj35t7FHWv2SY0awHGs60aQjS/j0D+WKh0q78k NEZz0cEdfOq8pICdDwkyGP+0aNgat71wgW4NSMaqLQxiEICRxApP1AYUxl8QqQd6 ClVRdIDPrAFY8NYm2Tdp/TofH4dfTAGtJKZapUgPQJEvwMbac9lSYdafHqzFRDGN oR4FJgULf4bqQNLkmXeV9GtVIiMkDZ/ch+OnDbjMMU4k0o6iL5grYbuMe8kI2KEB OwoHl7QTgJFtsDsYr40o1dP40EZ27L4QKkE5KwD
  • From Stefan Ram@21:1/5 to Ulrich Goebel on Sat Jun 29 20:48:58 2024
    Ulrich Goebel <ml@fam-goebel.de> wrote or quoted:
    a class can have methods, and it can have attributes, which
    can hold a function. Both is well known, of course.

    Methods are attributes too:

    Main.py

    class c:
    def m():
    pass

    print( *( name for name in dir( c )if not name.startswith( '__' )))

    sys.stdout

    m

    .

    class MyClass:
    def __init__(self):
    functionAttribute = None

    Your local name "functionAttribute" is not an attribute of "MyClass".

    mc = MyClass()
    mc.functionAttribute = function

    Above, you only add an attribute to "mc", not to "MyClass".

    By the way: in my usecase I want to pass different functions
    to different instances of MyClass.

    class MyClass:
    def __init__( self ):
    self.function = None
    def accept( self, function ):
    self.function = function

    mc = MyClass()
    mc.accept( print )

    mc_1 = MyClass()
    mc_1.accept( dir )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mats Wichmann@21:1/5 to Ulrich Goebel via Python-list on Sat Jun 29 14:03:13 2024
    On 6/28/24 10:08, Ulrich Goebel via Python-list wrote:

    By the way: in my usecase I want to pass different functions to different instances of MyClass. It is in the context of a database app where I build Getters for database data and pass one Getter per instance.

    If I understood what you're trying to accomplish, you could take a look
    here (possibly a bit complex for what you need).

    https://refactoring.guru/design-patterns/strategy/python/example

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Ulrich Goebel via Python-list on Sat Jun 29 16:01:49 2024
    On 6/28/2024 12:08 PM, Ulrich Goebel via Python-list wrote:
    Hi,

    a class can have methods, and it can have attributes, which can hold a function. Both is well known, of course.

    My question: Is there any difference?

    The code snipped shows that both do what they should do. But __dict__ includes just the method, while dir detects the method and the attribute holding a function. My be that is the only difference?


    class MyClass:
    def __init__(self):
    functionAttribute = None

    def method(self):
    print("I'm a method")

    def function():
    print("I'm a function passed to an attribute")

    mc = MyClass()
    mc.functionAttribute = function

    mc.method()
    mc.functionAttribute()

    print('Dict: ', mc.__dict__) # shows functionAttribute but not method print('Dir: ', dir(mc)) # shows both functionAttribute and method


    By the way: in my usecase I want to pass different functions to different instances of MyClass. It is in the context of a database app where I build Getters for database data and pass one Getter per instance.

    Thanks for hints
    Ulrich


    https://docs.python.org/3/library/functions.html#dir -

    object.__dict__¶
    A dictionary or other mapping object used to store an object’s
    (writable) attributes.

    dir(object)
    ...
    With an argument, attempt to return a list of valid attributes for that
    object.

    "functionAttribute" is a class method, not an instance method. If you
    want an instance method:

    class MyClass:
    def __init__(self):
    functionAttribute = None
    self.instance_functionAttribute = None

    def method(self):
    print("I'm a method")

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Stefan Ram on Sat Jun 29 22:55:54 2024
    On 29 Jun 2024 20:48:58 GMT, Stefan Ram wrote:

    Ulrich Goebel <ml@fam-goebel.de> wrote or quoted:
    a class can have methods, and it can have attributes, which can hold a >>function. Both is well known, of course.

    Methods are attributes too:

    They are indeed:

    class A:
    def set_x(self, x) :
    self.x = x
    #end set_x
    #end A

    class B:
    pass
    #end class B

    def set_x(self, x) :
    self.x = x
    #end set_x

    B.set_x = set_x

    AInst = A()
    AInst.set_x(9)
    print(AInst.x)
    BInst = B()
    BInst.set_x(4)
    print(BInst.x)

    Output:

    9
    4

    No fundamental difference between the two ways of attaching a method to a class.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dieter.maurer@online.de@21:1/5 to Ulrich Goebel on Sun Jun 30 19:58:37 2024
    Ulrich Goebel wrote at 2024-6-28 18:08 +0200:
    Hi,

    a class can have methods, and it can have attributes, which can hold a function. Both is well known, of course.

    My question: Is there any difference?

    I think you should make the distinction "class versus instance attribute" rather than "mether versus function".

    If you look at the `__dict__` of an instance, you see only the
    instance variables (the class's `__dict__` gives you the (most) attributes
    of the class).

    You can access (most) class attributes via an instance;
    if a function is accessed in this way, it becomes (typically) a method.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)