Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196346 > unrolled thread

Difference method vs attribut = function

Started byUlrich Goebel <ml@fam-goebel.de>
First post2024-06-28 18:08 +0200
Last post2024-06-29 22:55 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Difference method vs attribut = function Ulrich Goebel <ml@fam-goebel.de> - 2024-06-28 18:08 +0200
    Re: Difference method vs attribute = function (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-29 22:55 +0000

#196346 — Difference method vs attribut = function

FromUlrich Goebel <ml@fam-goebel.de>
Date2024-06-28 18:08 +0200
SubjectDifference method vs attribut = function
Message-ID<mailman.179.1719687158.2909.python-list@python.org>
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>

[toc] | [next] | [standalone]


#196351 — Re: Difference method vs attribute = function (Posting On Python-List Prohibited)

FromLawrence D'Oliveiro <ldo@nz.invalid>
Date2024-06-29 22:55 +0000
SubjectRe: Difference method vs attribute = function (Posting On Python-List Prohibited)
Message-ID<v5q3dq$4pv6$2@dont-email.me>
In reply to#196346
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web