Sujet : Re: Difference method vs attribut = function
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 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 )