Tutorial 2 : OOPs in Python — more examples of objects and classes

Photo by Hitesh Choudhary on Unsplash

Missed previous tutorial ? : Click hare

In previous tutorial, we learnt about creating classes and objects. Now we will learn about what more we can do while creating objects and classes.

Feel free to navigate directly to any topic given below :

Example 1 to 3

Example 4 : Defining multiple methods in a class

Example 5 : Assigning the default value to the attribute

Example 6 : Class attribute and Instance attribute

Example 7 : Alternate way of specifying class attribute

Example 8 : Defining the attribute without writing the attribute in init method

Example 9 : Accessing the attribute value without self keyword

Example 4 : Defining multiple methods in a class

Note : In OOPs while creating Class, ‘self’ argument is always present for every method(function).

Example 5 : Assigning the default value to the attribute

In previous example we saw that when one fails to pass the value for attribute then it fetches the error. So what do we do so that error will not be fetched even if one fails to pass value to the attribute — we assign default value to the attribute using assignment operator (‘=’ sign) .

Example 6 : Class attribute and Instance attribute

In above examples, as we know value of variable ‘pi’ will always be 3.14, so we can define that outside init method as a Class attribute. Let’s see it using example given below :

Let’s now understand the difference between Class attribute and instance attribute :

1)class attribute -

  • class-specific
  • same for all objects
  • defined outside method

2) instance attribute -

  • object/instance-specific
  • can be different for different objects
  • defined inside method

Example 7 : Alternate way of specifying class attribute

self.class_attribute = class_name.class_attribute

self.pi = Circle.pi

This is useful when you have a hundreds of lines of code and you want to update or locate the attribute, then Circle.pi can make it easy to locate.

Example 8 : Defining the attribute without writing the attribute in init method

Suppose you have written a software and after few years if you want to add any feature in a function then you can update it without writing that attribute in init method. If you add feature in init method then you will have to update all the objects wherever that class is used otherwise it will fetch error that feature is not defined . This makes OOPs very powerful.

Example 9 : Accessing the attribute value without self keyword

Accessing the attribute value without self keyword is possible only if that attribute is defined inside the same method where it is being used.

Let’s see it using the example :

Congratulations !!

You have completed second tutorial . We will learn about basic building blocks — Inheritance, Polymorphism, Encapsulation in upcoming tutorials.

Take a deep breathe and move to the next tutorial.

--

--

--

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create High Availability Architecture with AWS CLI

“Git merge …” succeeded, wait which branch I am on again? OMG, not master… “Git reset…”

Git and GitHub Workshop

Tunnelling display/GUI through VNC and X11 from Linux to Windows

Creating a Game in 48 Hours — Ludum Dare 48 Post Mortem

The title image of the story, a screenshot of the game with buildings, trees, gold, and minions.

Product tear down : TeamViewer

His secret to becoming a successful developer is simple

lead skymatics developer

Unlock the Power of E-commerce in a Headless World

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

More from Medium

Tutorial 4: OOPs in Python — Polymorphism

Python Unknown (Tips and Tricks) — Part 1

Fibonacci Series in Python | 5 Best Programs

Can You Solve This in Python?