Jun-29-2024, 07:57 PM
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
Thanks for hints
Ulrich
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 this is the only difference?class MyClass:
def __init__(self):
self.functionAttribute = lambda x: ''
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 methodBackground to my question: In a context of a database app I want to pass different functions to different instances of MyClass. I'm building Getters for database data and pass one Getter per instance.Thanks for hints
Ulrich
