2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

What is class in python

Last updated at Posted at 2019-06-12

A memorandum for myself. Focused on understanding at the cost of accuracy.

class is

A blue print for creating objects.

Introduction

As introduced in the previous post, object is a package of methods and data. class is a blue print or template to create those objects. Every time you call a class, you will create a new object (which is called Instance.

Example

class.py
# declare that this is a blue print of "glass_of_water"
class glass_of_water: 
    # first, you have water as your data
    my_data = 'water'
    # this is called attribute

    # then, define the method
    def pour(self):    
        # this is the actual action of the method
        print('pour ' + self.my_data)   

Let's try to run this code.
First, create an object with the blue print of glass_of_water.

object1 = glass_of_water()

Now you run it.

>>> object1
<__main__.glass_of_water at 0x7f7fb0a76198>

You get nothing because you didn't specify what to do, i.e. the method.
Let's do it again.

>>> object1.pour()
pour water

Reference

https://www.hackerearth.com/practice/python/object-oriented-programming/classes-and-objects-i/tutorial/
https://qiita.com/lamplus/items/351145a81f798ecec80b
https://qiita.com/hiroyuki_mrp/items/140d47df01553091d59a

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?