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")
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.
a class can have methods, and it can have attributes, which
can hold a function. Both is well known, of course.
class MyClass:
def __init__(self):
functionAttribute = None
mc = MyClass()
mc.functionAttribute = function
By the way: in my usecase I want to pass different functions
to different instances of MyClass.
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.
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> 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:
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?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 08:05:53 |
Calls: | 10,387 |
Calls today: | 2 |
Files: | 14,058 |
Messages: | 6,416,655 |