How to define a class in python

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

2 answers 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")

__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, 2019 by Mohammad
• 3,230 points
0 votes

Class use the special word  class for declaration. Inside the class __init__() helps to define local variable for class.for more information check this tutorial link  may be it helps you

answered Jun 6, 2020 by anonymous
• 540 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
• 58,120 points
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
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, 2019 in Python by anonymous
+1 vote
2 answers

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

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
0 votes
2 answers
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,159 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
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, 2019 in Python by Mohammad
• 3,230 points
0 votes
2 answers

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

there is sum() function as a built ...READ MORE

answered Oct 24, 2020 in Python by anonymous