DEV Community

Cover image for Python | OOP | Classes
Tahir Raza
Tahir Raza

Posted on

4 3

Python | OOP | Classes

Recently, I have realised that it is really hard for the beginners to follow the official documentations and most importantly, make sense out of it.

Since Python is one of the easy to use language and a lot of people want to get started with it, I think it is a good idea to share the knowledge in plain English.

For this reason, I am starting this blog post series to explain Python's object oriented model.

Python's object oriented model has the following basic characteristics:

  • It is a mixture of the class mechanisms found in C++ and Modula-3.
  • Classes themselves are objects. This helps for importing and renaming.
  • Class members are public (Methods and Data Members both).
class Example:
    classAttribute = 12345

example = Example()

print(example.i)
Enter fullscreen mode Exit fullscreen mode
  • Every class method needs to have self as the first argument, which is provided implicitly by the call.
class Example:
    classAttribute = 12345

example = Example()

print(example.i)
Enter fullscreen mode Exit fullscreen mode
  • Aliasing: Objects have singularity, and multiple names can be bound to the same object. This is known as aliasing in other languages. Simply speaking, if you pass on object to a function as a argument and change the object inside that function, it will be changed in the main function as well.
from typing import Type

class Counter:
    count = 0

def changeCount(counter: Type[Counter]):
    counter.count = 5

counter = Counter()

print(counter.count)
# 0

changeCount(counter)

print(counter.count)
# 5
Enter fullscreen mode Exit fullscreen mode
  • Most namespaces are currently implemented as Python dictionaries.

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video 🎥

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay