Why does delegation performed in Python?

Answer Posted / chaitanya

Delegation is a technique that is used in object oriented programming. This is used to show the object and the behavior of the methods that are used. The class can be created to provide an implementation of the method that allows the method to be referenced. The delegate is having the parameter and the return value in an object. It also allows the methods to be passed as parameters and allow the defining of the callback methods that can be grouped together in multiple methods. These methods can be called from a single event. The example shows a class that captures the behavior of the file and converts data from lower to uppercase.

class upcase:

def __init__(self, out):

self._out = out

def write(self, s):

self._outfile.write(s.upper())

def __getattr__(self, name):

return getattr(self._out, name)

The write() method that is used in the upcase class converts the string to the uppercase before calling another method. The delegation is being given using the self.__outfile object.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is py checker in python?

467


What is the input function in python?

475


How to display the contents of the text file in reverse order?

454


Which is best python or r?

426


What is the most "pythonic" way to iterate over a list in chunks?

459






What is a variable in python?

476


How is the Implementation of Pythons dictionaries done?

501


Whenever python exists why does all the memory is not de-allocated / freed when python exits?

478


Is python a nonetype?

452


What is the __ init __ method used for?

454


Can we concat bytes to str?

484


What is sphinx python?

455


What is an elif in python?

418


How to remove whitespaces from a string in python?

466


How to make any variable private in python ?

449