Re: Difference method vs attribut = function

Liste des GroupesRevenir à cl python 
Sujet : Re: Difference method vs attribut = function
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 29. Jun 2024, 22:48:58
Autres entêtes
Organisation : Stefan Ram
Message-ID : <attributes-20240629214445@ram.dialup.fu-berlin.de>
References : 1
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 )

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