Python class example and explanation | applied electronics engineering


Python class example and explanation

By Applied Electronics - Friday, July 1, 2016 No Comments

In python programming language, everything is objects. Modules, classes, functions or methods, attributes, dictionary, list etc are objects. The difference is about concept. That is, modules, classes, functions etc are concepts but in reality, in terms of how they are represented and accessed in computer memory is in terms of objects with some ID and memory address. This becomes apparent when you have done fairly amount of exercises. 

In the most basic words, a python class is a way of grouping functions and attributes. Any time you want to use a specific class function or attributes you create an instance/object of the class. The instance or object uses a period/dot "." character to access the class function or attributes.

Before showing some example of python classes, we have to know the relationship of different concepts of python programming language. That is how a python program or script, packages, modules, classes, functions etc are interrelated. The following picture depicts the relation between them-

As you can see, a python program may consist of package(s). The package is logical grouping of modules. Each module may contain a class or function or properties only or each module may contain class, function and/or properties. Again a class contains functions and properties.

How to write Class in Python programming language

To write a class in python programming language we use the pre-defined keyword "class".
 Here is an example of basic python class-

class People:
    def speak(self,name):
        print("my name is", name)
    def ask(self):
        input("what is your name?")

In the above python class example code, the class is called People. It has two methods called speak( ) and ask( ). The def keyword is required to declare a method/function. All methods and properties of a class in python have self as its first argument and this is mandatory. The keyword self basically is required for create different objects/instances using the same method of a class. Meaning, if we create two objects(instances) of the same class method and use the same method how does python identify different objects method. 

But the keyword self has also another reason to be there. It indicates within the class the method and properties. For example, using self keyword we can use one variable within a class in method of the same class.

Now how to use the above People class?

Whenever you want to use class's methods such as speak or ask, first you declare an instance of the class(this is creation of an object).  This is shown below.

>> from people import people

>> jon = people()

>> jon.speak("Jon")
my name is Jon

 The first line is required to tell python where the class is located. The module name is people and the class name is also people. The syntax is from <module> import <class>. 

The second line creates an instance/object called jon of the class people. 

In the third line, we used the method called speak( ) defined in class people. And we get the output my name is Jon.

Similarly, we can use the second method ask( ) as follows.

from people import people>> jon.ask()

what is your name?

See more python class examples ->


No Comment to " Python class example and explanation "