Pages

Python Class: A simple and clear explanation.

I have been trying to understand class in python and failed to use it any useful purpose. Then, I came across this  webage where I got a clear understanding of Class in Python. To understand this, I went through so many pages and blogposts. But I didn't get it. 

https://micropyramid.com/blog/understand-self-and-__init__-method-in-python-class/

See the code blow (taken from above link). 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Car(object):
  """
    blueprint for car
  """

  def __init__(self, model, color, company, speed_limit):
    self.color = color
    self.company = company
    self.speed_limit = speed_limit
    self.model = model

  def start(self):
    print("started")

  def stop(self):
    print("stopped")

  def accelarate(self):
    print("accelarating...")
    "accelarator functionality here"

  def change_gear(self, gear_type):
    print("gear changed")
    " gear related functionality here"

This code is an example for a class in python language.

As you can see, this is a collection of functions (def) grouped in a class named Car. 

What is the use of Class?

Class is used to collect list of definitions and provide a functionality to crosscommunicate with all other definitions inside the Class. 

What is the best explanation for Class? Do you have any link? 

Comment and provide link here. 

Thanks. 

No comments:

Post a Comment

You may be interested in these posts

Error in image file conversion: convert-im6.q16: not authorized `test.eps' @ error/constitute.c/WriteImage/1037.

This error is because of the vulnerability. This allows remote execution of code using image formats. So, some Linux distributions by defaul...