Sign in

Classes & Objects in python

Class:-

What is a class?

class ClassName:
<statement-1>
.
.
.
<statement-N>

Example:

Object :-

Declaring Objects (Also called instantiating a class)

class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')print(Person.age)
10
x = MyClass()
def __init__(self):
self.data = []
x = MyClass()
>>> 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)

References: