Showing posts with label python function program. Show all posts
Showing posts with label python function program. Show all posts

Tuesday, December 1, 2020

,

Example program for map() function in python

 map() function in Python applies a given function to every item of an iterable and give a list of results.

Syntax:-

map(function, iterable, ...)

Parameter For map() function in python 

map() function use two Parameter these are follow : - 

  •  function is a first Parameter map() function passes every item of iterable to the given function.
  • iterables are second which is going to to be mapped.

 Return value : - map() function in python return the list of result.

Example of map() function in python

def calculateSquare(n):
    return n*n


numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)

# converting map object to set
numbersSquare = set(result)
print(numbersSquare)

OUTPUT : -

map() function output
 map() function output

Well Python lambda Function commonly used with map () python function.



Monday, November 16, 2020

, , ,

Example program for super function in python

 super() function in python return the proxy object it's also a temporary object of the superclass and this object help us to access the method of base class.

Super function has two use :-

  • This function Working with Multiple Inheritance
  • Help us to avoid base class name explicitly

Example of super function in python:-

if it use single inheritance then this function help us to refer base class by super()
class Mammal(object):
  def __init__(self, mammalName):
    print(mammalName, 'is a warm-blooded animal.')
    
class Dog(Mammal):
  def __init__(self):
    print('Dog has four legs.')
    super().__init__('Dog')
    
d1 = Dog()

OUTPUT

Dog has four legs.
Dog is a warm-blooded animal.

We can also change the name of base class Let see

# changing base class to CanidaeFamily
class Dog(CanidaeFamily):
  def __init__(self):
    print('Dog has four legs.')

    # no need to change this
    super().__init__('Dog')

Well super function in python is an call method for base class via delegation and that is called indirection and this happen at run then time in between this time we can also use the other base classes at different times.

Example program for super() with Multiple Inheritance

class Animal:
  def __init__(self, Animal):
    print(Animal, 'is an animal.');

class Mammal(Animal):
  def __init__(self, mammalName):
    print(mammalName, 'is a warm-blooded animal.')
    super().__init__(mammalName)
    
class NonWingedMammal(Mammal):
  def __init__(self, NonWingedMammal):
    print(NonWingedMammal, "can't fly.")
    super().__init__(NonWingedMammal)

class NonMarineMammal(Mammal):
  def __init__(self, NonMarineMammal):
    print(NonMarineMammal, "can't swim.")
    super().__init__(NonMarineMammal)

class Dog(NonMarineMammal, NonWingedMammal):
  def __init__(self):
    print('Dog has 4 legs.');
    super().__init__('Dog')
    
d = Dog()
print('')
bat = NonMarineMammal('Bat')

OUTPUT

super function in python
Example program for super function in python

 

 

Method Resolution Order (MRO)For super() function in python

MRO is use to maintain the order at the persence of the multiple inheritance and ir can also view by using the __mro__ attribute.

>>> Dog.__mro__
(<class 'Dog'>, 
<class 'NonMarineMammal'>, 
<class 'NonWingedMammal'>, 
<class 'Mammal'>, 
<class 'Animal'>, 
<class 'object'>)

Saturday, November 14, 2020

, ,

Example program for setattr() function in Python

setattr() function in Python use to set the value of an attribute.

setattr() function
setattr() function in Python

 

Syntax : -

setattr(object, name, value)

If you want to read the object then use the getattr() function.

  • Parameters For setattr():- 

It takes three parameters and these are:-

  • Object : - the object whose value is going to be set in python program.
  • Name - set the name of attribute.
  • value - Desire value given to the attribute

setattr() function in python doesn't return the value.

Example program for setattr() function in Python

class Person:
    name = 'Adam'
    
p = Person()
print('Before modification:', p.name)

# setting name to 'John'
setattr(p, 'name', 'John')

print('After modification:', p.name)

Output:-

setattr() function in Python
program for setattr() function in Python