Difference method vs attribut = function

Liste des GroupesRevenir à cl python 
Sujet : Difference method vs attribut = function
De : ml (at) *nospam* fam-goebel.de (Ulrich Goebel)
Groupes : comp.lang.python
Date : 28. Jun 2024, 18:08:54
Autres entêtes
Message-ID : <mailman.179.1719687158.2909.python-list@python.org>
References : 1
User-Agent : Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu)
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>

Date Sujet#  Auteur
28 Jun 24 * Difference method vs attribut = function3Ulrich Goebel
29 Jun 24 `* Re: Difference method vs attribut = function2Stefan Ram
30 Jun 24  `- Re: Difference method vs attribute = function (Posting On Python-List Prohibited)1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal