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
1,208 views

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")
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, 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.

answered Jun 6, 2020 by anonymous
• 500 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,020 points
2,355 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
1,268 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, 2019 in Python by anonymous
5,862 views
+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
12,394 views
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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
+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,790 points
4,077 views
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
897 views
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 25, 2020 in Python by anonymous
23,847 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP