How to define a class in python?

0 votes
What is __init__ () function while declaring a class in python?
Jul 23 in Python by Waseem
• 4,480 points
17 views

1 answer to this question.

0 votes

Following is a code to define a class in python:

class Parent:       
      def __init__(self, fname, lname):
          self.firstname = fname           
          self.lastname = lname          
      def view(self):           
          print(self.firstname , self.lastname)
class Child(Parent):
     def __init__(self, fname, lname):
         parent.__init__(self, fname, lname)          
         self.age = "20"
     def view(self):
         print(self.firstname , self.lastname , self.age)
ob = Child("Guido" , "Rossum")
ob.view()

__init__() function is called everytime a class is being used to make an object. In this Program, the child class init function overrides the parent class init function.   

answered Jul 30 by Mohammad
• 3,060 points

Related Questions In Python

0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,900 points
57 views
0 votes
1 answer

How to add a new line in Python?

You can use '\n' for a next ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
106 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2 in Python by anonymous
212 views
0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
2,185 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 283 views
+4 votes
6 answers
0 votes
1 answer

how to delete a file in python?

to delete a file import os os.remove('filename') this will delete ...READ MORE

answered Mar 18 in Python by Mohammad
• 3,060 points
21 views
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 3,060 points
173 views