Unable to understand code -newbie

Question:

I am new to python ( started 1 week ago) and this is the first time i am doing coding so i am not able to understand fairly simple things as well.

can you explain this function to to me? i understand that a function is being defined with 2 input required self and my_object, but what is happening next? please explain like you would to a newbie.

class chain():
    def __init__(self, my_object):
        self.o = my_object

    def __getattr__(self, attr):
        x = getattr(self.o, attr)
        if hasattr(x, '__call__'):
            method = x
            return lambda *args: self if method(*args) is None else method(*args)
        else:
            prop = x
            return prop
Asked By: Riya Arora

||

Answers:

The class chain has a constructor that takes an argument my_object and assigns it to an instance variable self.o.

The method __getattr__ is a special magic method that has been overridden to delegate calls to the initial my_object variable we first received.

The result of the delegated call is checked for a method named __call__. If present, it is called and the returned value is returned. If not, the value itself is returned as-is.

Answered By: AlanSTACK

Firstly, chain is not a Function, it’s a Class.

A class in simple words is a definition of an object. (say Car)

Now the __init__ function of the class simply defines what’s “in it” meaning what variables or properties does it has. Say for example a class Car:

class Car:
    def __init__(self,maxspeed,color):
        self.speed = maxspeed #So what's defined under **__init__** is a property of a class.
        self.color = color

So here Class car has speed and color as variables(or attributes or properties)

Now there are methods , of simply function that control the behaviour of the object and it’s functionalities.

class Car:
    def __init__(self,maxspeed,color):
        self.speed = maxspeed #So what's defined under **__init__** is a property of a class.
        self.color = color
    def accelarate(self):   #Method to increase the speed of car object.
        self.sepped = self.speed + 10 

Now the method you have is a magical one , __getattr__

Say a scenario where you want to acess the brand of the car , now you haven’t define self.brand in it’s __init__ function so you you’ll get an error when you call it like:

>>>red_car = Car(100,red) #Creating an object named red_car of class Car
>>>red_car.color
>>>'red'
>>>red_car.brand
>>> Attribute Error , Class car dosen't has attribute brand

Now remove this error when calling an undefined property for a object or put simple we tell tell the class what to do if an undefined variable is called we use the method __getattr__.

class Dummy(object):
    def __getattr__(self, attr):
        return attr.upper()

d = Dummy()
d.does_not_exist # 'DOES_NOT_EXIST'
d.what_about_this_one  # 'WHAT_ABOUT_THIS_ONE'

In the above code does_not_exist property (attribute) is NOT define but still we are not getting error as the getattr catches it and does as instructed. In this case it catches attr capitalises it and returns it rather than throwing an error in your face.

Answered By: Ubdus Samad
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.