
If we want to do object oriented programming with Python, we must know how to create classes in Python. In this post, I'll learn how to define a Hive class, its methods and properties in python and check it with an example.
Object-oriented programming helps us write more readable code closer to the real world. We know that classes are like a template for objects. Each instance of a class is called an object.
Each class can have different properties. These properties are called state or attribute. Also, each object can show its own behaviors; These behaviors are defined as methods in the class.
For example, each of us is an instance (object) of the human class. Each of us has our own unique properties. From skin color and nationality to height, weight and age. Also, we can all do things like walk, eat or talk.
Based on this example, in the following post, I'll implement a simple class of Hive in Python.
Class definition in Python
Suppose I want to create a class called Hive. This class represents the existence of the $Hive in our system.
The general structure of class definition in Python is as follows. First, I write the keyword class
, and then I write the name of the class with a capital letter and determine the beginning of the class range with :
.
Similar to any other structure in Python, the indentation of the code in the class structure is important.
class Hive:
# class body here...
To define the description of the class, we write the descriptive docstring in the first line of the body in the form of a text string; Like the following class:
class Hive:
"""represent hive identity"""
# class body here ....
Class property in Python
The properties of a class (property) maintain the state and values associated with the created instances of that class. In fact, these properties are variables in which we put the required information.
To define a class property in Python from the beginning, we do the same thing as defining a Python variable. In this way, in the body of the class, we write the name of the variable (property) and value it.
class Hive:
name = "Hive"
nationality = "blockchain"
score = 100
# some codes
Of course, sometimes we may create a variable in one of the class methods according to our needs. We can do this with the self
keyword.
Define class methods
Class methods in Python are the behaviors of an object of the class. To define methods, we do exactly the same as defining functions in Python. That is, after the keyword def
, we write the name of the desired method and, if necessary, specify its input parameters.
In the code below, I've defined a method called say_hello()
that prints "hello"
to the output.
class Hive:
name = "Hive Blockchain"
score = 100
def say_hello(self):
print( "Hello!" )
Note that to define methods in Python classes, at least one input argument, which is usually defined as self
, should be considered for it.
The self
variable in Python
In order to access the properties of the same object in the class methods, we use self
. This value points to our same object and we can treat it like an object.
In the following class, I've defined the set_name()
method. This method takes an input from the user and changes the name
property of the object.
class Hive:
username = None
score = 100
def set_name(self, name):
self.username = name
In the body of Python class methods, we can do the processing we want. Let me modify the above class a bit. I want to change the username
when the set_name()
method is called, if the username
is None
, otherwise print an error. (Condition in Python)
class Hive:
username = None
score = 100
def set_name(self, name):
if self.username == None:
self.username = name
else:
print("You Cann't Change username Attribute!")
Using classes in Python
Now that we have created a simple class, we can create an object
from it. When an object is created, it will have a unique id
in the computer memory. Therefore, all objects are different from each other and their properties are different from each other.
Create an object from a class
To create an object from a class in Python, it's enough to write the name of the class as a function. For example, to create a new object from the Hive class
, we do the following:
user = Hive()
With this, a value is stored in the user
variable, with the help of which we can access the methods and properties of the object. If we check the type()
of this object, a result similar to the image below will be returned to us:

By putting a point (.
) after the name of the object, we can call and use its methods and properties. In the code below, after creating an instance of the Hive class
, I've defined its username
and then printed the value of its score
property. (Print in Python)
user = Hive()
user.set_name("albro")
print( user.score )
__init__ constructor
in Python
When we want to create an object of a class in Python, a method is called. This method is called constructor
. When we call Hive()
, we are actually executing the constructor method of the Hive class
.
If no constructor method is defined for a class, there is no problem. But usually, this method is used for the initial tasks of creating an object from a class (such as initializing properties).
To define the constructor method of the class in Python, we act as follows. The following constructor is the simplest class constructor.
class Hive:
username = None
score = 100
def __init__(self):
print("Hive Object Created")
We can define input arguments for the __init__
method. For example, in the following class, I set the username
and power
of the user when the object is created.
class Hive:
username = None
power = 100
def __init__(self, name, power):
self.username = name
self.power = power
def say_hello(self):
print("Hello", self.username)
You can see that with the help of self
, we can define and value variables that are not already defined in the class (such as power
). Once initialized, this variable will be available to the entire Python class.
When we want to create an object of this class, we must also specify its input arguments. Therefore, to create an object from the above class, you must do the following:
user = Hive("albro", 100)
user.say_hello()
If we consider the Hive as an object, can you tell what properties and methods can be defined for it?
Each person in the Hive has a name, power, etc., and is able to do things like upvote or downvote for posts, comments, trades, etc!