- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is difference between self and __init__ methods in python Class?
The word 'self' is used to represent the instance of a class. By using the "self" keyword we access the attributes and methods of the class in python.
"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.
Find out the cost of a rectangular field with breadth(b=120), length(l=160). It costs x (2000) rupees per 1 square unit
class Rectangle: def __init__(self, length, breadth, unit_cost=0): self.length = length self.breadth = breadth self.unit_cost = unit_cost def get_area(self): return self.length * self.breadth def calculate_cost(self): area = self.get_area() return area * self.unit_cost # breadth = 120 units, length = 160 units, 1 sq unit cost = Rs 2000 r = Rectangle(160, 120, 2000) print("Area of Rectangle: %s sq units" % (r.get_area()))
This gives the output
Area of Rectangle: 19200 sq units Cost of rectangular field: Rs.38400000
- self in Python class
- Difference between title() and wm_title() methods in Tkinter class
- What is the difference between re.match(), re.search() and re.findall() methods in Python?
- What is the difference between re.findall() and re.finditer() methods available in Python?
- What is the difference between scipy.cluster.vq.kmeans() and scipy.cluster.vq.kmeans2() methods?
- What is the difference between non-static methods and abstract methods in Java?
- What is the difference between re.search() and re.findall() methods in Python regular expressions?
- What is the difference between String.Copy() and String.Clone() methods in C#?
- What is the difference between String.Copy() and String.CopyTo() methods in C#?
- What is the difference between Write() and WriteLine() methods in C#?
- What is the difference between Trim() and TrimStart() methods in C#?
- What is the difference between Read() and ReadLine() methods in C#?
- What is the difference between switchClass() and toggleClass() methods in jQuery?
- What is the difference between $.closest() and $.parents() methods in jQuery?