Class and Objects - Object Oriented Programming in Python

Search for a command to run...

No comments yet. Be the first to comment.
Hi, I will add all python projects, blogs, awesome codes here and hey pythoneers you will enjoy for sure.
Instance and Methods in Python Object Oriented Programming We learnt in Last blog Class and Objects - Object Oriented Programming in Python how to create Class and objects we learnt about constructors. But after learning instant objects, what to do w...
I had problems like when we switch branches, UI files like drawables, icons, xml files etc gets in android studio and caches and even after doing basic invalidate caches restart etc it didnt worked, so after compiling all steps leds to soltion , here...

In today's discussion, we delve into implementing bottom navigation in Jetpack Compose, which can be particularly challenging when combined with nested navigation. While several methods are available, developers often encounter difficulties with navi...

A Clean Architecture Guide for Building Modern Android Apps

Master NavController, NavHost, and More

Introduction LangChain is an open-source framework for developing applications powered by large language models (LLMs). It provides a high-level API that makes it easy to chain together multiple LLMs, as well as other data sources and tools, to creat...

Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics. It is a mixture of the class mechanisms found in C++ and Modula-3.
Python classes provide all the standard features of Object Oriented Programming:
The simplest form of class definition looks like this:
class ClassName:
<statement-1>
.
.
.
<statement-N>
When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.
Define Class Code:
class Dog:
pass
Dog is the name of Class here.
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values.
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created.
So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example (assuming the above class):
x = MyClass()
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:
def __init__(self):
self.data = []
When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance. So in this example, a new, initialized instance can be obtained by:
Example of having arguments in [__init__()]
>>> class Complex:
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5) # Output
A program to show example of class creation and object creation and passing arguments:
class ImClass():
def __init__(self,object_name,object_id):
self.name=object_name
self.id=object_id
abc=ImClass("Iron man",2)
xyz=ImClass("Spidey",4)
To access a variable we can do like this:
print(abc.name)
print(abc.id)
print(xyz.name)
print(xyz.id)
Output:
Iron man
2
Spidey
4
Here in this blog we understands about class and objects and init() which can be called as constructor like another languages.
In next Blog we will understand more about variables and functions in classes and their scope.