The difference between a method call and attribute access

I am very new to Python and I am using Python 3.3.1.

class Parent: # define parent class parentAttr = 100 age = 55 def __init__(self): print ("Calling parent constructor") def setAttr(self, attr): Parent.parentAttr = attr class Child(Parent): def childMethod(self): print ('Calling child method') 

Now i will create

 c=child c.[here every thing will appear methods and attr (age,setAttr)] 

How can I distinguish between methods and attributes? I mean, when do I use c.SetAtrr(Argument) and c.SetAtrr=value ?

+6
source share
1 answer

Ways are also attributes. They just become callable objects.

You can determine if an object can be called using the callable() function:

 >>> def foo(): pass ... >>> callable(foo) True >>> callable(1) False 

When you call the method, you look at the attribute (a getattr() ) and then call the result:

 c.setAttr(newvalue) 

- two steps; find the attribute (which in this case searches for the class attribute and processes it as a descriptor), then calls the received object, the method.

When you assign an attribute, you rebuild this name to a new value:

 c.setAttr = 'something else' 

- operation setattr() .

If you want to intercept the receipt and setting of attributes in instances of your class, you can provide resource access attributes , __getattr__ , __setattr__ and __delattr__ .

If you want to add a method to an instance, you will have to consider the function as a handle object that creates the method object

 >>> class Foo: pass ... >>> foo = Foo() # instance >>> def bar(self): pass ... >>> bar <function bar at 0x10b85a320> >>> bar.__get__(foo, Foo) <bound method Foo.bar of <__main__.Foo instance at 0x10b85b830>> 

The return value of function.__get__() , if an instance and a class are given, is a related method. Calling this method will invoke the base function with self bound to the instance.

And speaking of descriptors, the property() function also returns a descriptor, allowing you to have functions that behave like attributes; they can intercept the operations getattr() , setattr() and delattr() only for this attribute and turn it into a function call:

 >>> class Foo: ... @property ... def bar(self): ... return "Hello World!" ... >>> foo = Foo() >>> foo.bar "Hello World!" 

Access to .bar called the get bar attribute, which then calls the original bar method.

In almost all situations, you will not need the callable() function; you document your API, and provide methods and attributes, and your API user will find out without checking each attribute to see if it is callable. In properties, you have the flexibility of providing attributes that are actually callers.

+11
source

Source: https://habr.com/ru/post/946854/


All Articles