Python Forum
Difference between method and attribute holding a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between method and attribute holding a function
#1
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 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 method
Background 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
Reply
#2
Quote:But __dict__ includes just the method,
MyClass.__dict__ includes method(), but mc.__dict__ does not. method() is an attribute of the class, and only appears in the class's __dict__. mc is an instance of MyClass, and method is not an attribute of the instance. method() does not appear in mc.__dict__.

"def method(self):" is problem code. It should not be an instance method (should not have self as the first argument). self is not used, and having self prevents using the function unless you first make an instance of MyClass.

MyClass should be written this way.
class MyClass:
 
    def __init__(self):
        self.functionAttribute = lambda x: ''

    @staticmethod
    def method(text):
        print(text)

x = MyClass()
x.method("Called using an instance.")

MyClass.method("Called as a function in the MyClass namespace.")
Output:
Called using an instance. Called as a function in the MyClass namespace.
Reply
#3
(Jun-29-2024, 07:57 PM)ulrich Wrote: print('Dict: ', mc.__dict__) # shows functionAttribute but not method
Methods defined in the class body are part of the class attribute dictionary,not the instance dictionary.
# Instance dictionary
>>> mc.__dict__
{'functionAttribute': <function function at 0x000001B9B775EAC0>

# Class attribute dictionary,now see the <method>
>>> MyClass.__dict__
mappingproxy({'__dict__': <attribute '__dict__' of 'MyClass' objects>,
              '__doc__': None,
              '__init__': <function MyClass.__init__ at 0x000002413180EB60>,
              '__module__': '__main__',
              '__weakref__': <attribute '__weakref__' of 'MyClass' objects>,
              'method': <function MyClass.method at 0x000002413180EC00>})
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,368 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,799 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 2,190 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 5,032 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  class, attribute and method Frankduc 9 2,772 Feb-27-2022, 09:07 PM
Last Post: deanhystad
Photo Raspberry PI writing to RS485 driver, DIR pin not holding state zazas321 2 2,266 Oct-30-2020, 06:23 AM
Last Post: zazas321
  Building a method name in a function ffgth 9 3,546 Oct-19-2020, 01:21 PM
Last Post: buran
  function/method help myv5285 3 2,990 May-17-2020, 04:19 AM
Last Post: buran
  Accessing method as function object ClassicalSoul 2 2,143 Feb-14-2020, 09:31 PM
Last Post: wavic
  function vs method prateekshaw 2 2,307 Nov-14-2019, 07:00 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020