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

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'>)

Thursday, November 12, 2020

, ,

Example for getattr() function in python

 getattr() function in python is sued to get the value of an object attribute and it return the default value if no attribute of that object is found.

getattr() function in python
Example for getattr() function in python

 

The reason of using getattr() function because it return the default value.Now let see the basic syntax : -

getattr(object_name, attribute_name[, default_value])

Example of getattr() function in python

this program will help you to teach about how to use the getattr() function in python

class Student:
    student_id=""
    student_name=""

    # initial constructor to set the values
    def __init__(self):
        self.student_id = "101"
        self.student_name = "Adam Lam"

student = Student()
# get attribute values by using getattr() function
print('\ngetattr : name of the student is =', getattr(student, "student_name"))

# but you could access this like this
print('traditional: name of the student is =', student.student_name)

Why use getattr() function

  • This function helps to get the object attribute value with the help of Name of that attr. or you can also manually set the input attribute name by console.
  • This function also give the freedom for set the default value according to your need.

Wednesday, November 4, 2020

,

Example Program For enumerate() function in python

 enumerate() function in python adds counter to a iterable and returns it in the enumerate python program.

Syntax : -

enumerate(iterable, start=0)
  • Two parameters are used the first one is an object which is accept the iteration.
  • The second one is start parameters which is a starting point of counting but it's optional. If start is extinct the it's take 0 at start.
  •   enumerate() function return the object after adding the counter to an iterable.

Example Program for enumerate() in Python


grocery = ['bread', 'Tea', 'jam']
enumerateGrocery = enumerate(grocery)

print(type(enumerateGrocery))

# converting the gienvalue into a list
print(list(enumerateGrocery))

# changing the default counter
enumerateGrocery = enumerate(grocery, 10)
print(list(enumerateGrocery))

OUTPUT

Program for enumerate in Python
Program for enumerate() in Python



Tuesday, November 3, 2020

, , ,

Example Program For Complex Function In Python - Python Program

  •  Well Python not only handle the real number but also handle the complex numbers with the help of Complex function in python.
  • This complex function is so useful to manipulate mathematics problem's.
  • complex function in python return the real number from complex number or convert the string to a complex number. 
Example Program For Complex Function In Python
Example Program For Complex Function In Python

 

 Parameter : -

  •     real - real part. If real is extinct then it defaults to 0.
  •     imag - imaginary part. If imag is extinct then it defaults to 0.

Return the complex number.

Example of Complex function in Python To Create a complex number:-

z = complex(2, 3)
print(z)

z = complex(2)
print(z)

z = complex()
print(z)

z = complex('4-5j')
print(z)
(2+3j)
(2+0j)
0j
(4-5j)