xxxxxxxxxx
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
xxxxxxxxxx
class Person:
def __init__(self, _name, _age):
self.name = _name
self.age = _age
def sayHi(self):
print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
xxxxxxxxxx
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
xxxxxxxxxx
class IntellipaatClass:
a = 5
def function1(self):
print(‘Welcome to Intellipaat’)
#accessing attributes using the class object of same name
IntellipaatClass.function(1)
print(IntellipaatClass.a)
xxxxxxxxxx
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def greet(self, person_to_greet):
# person_to_greet will be another Person object
print(f"Hey {person_to_greet.name}, nice to meet you I'm {self.name}")
def ask_age(self, ask_from):
print(f"{self.name}: {ask_from.name}, How old are you?")
print(f"{ask_from.name}: i am {ask_from.age}")
# Creating a person object
tom = Person("Tom", 50, "Male")
# we can also create an object with keyword arguments
jack = Person(name="Jack", age=19, gender="Male")
# Here we call the greet method of tom, and we pass the Jack Person Object Created above
tom.greet(jack)
# if we call the greet method of jack and pass the Tom person object, then jack greets tom
jack.greet(tom)
# Here Jack will ask age of tom, and tom will reply with his age
jack.ask_age(tom)
xxxxxxxxxx
class class_name:
def __init__(self):
# class variables(can be used all over the class)
pass
first_class = class_name() # creating the class
xxxxxxxxxx
class MyClass:
def __init__(self, value):
self.value = value
# Creating an instance of MyClass
my_object = MyClass(10)
# Printing the value attribute of my_object
print(my_object.value)
xxxxxxxxxx
class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y
def my_method(self):
return self.x * self.y
xxxxxxxxxx
class employee:
name = ''
id = 0
department = ''
def pay(self):
print(self.name, self.id, self.department)
s = employee()
s.name = 'burhan'
s.id = 0o2234523
s.department = 'Development'
s.pay()
b = employee()
b.name = 'Abdullah'
b.id =12423432857
b.department = 'Software Engineer'
b.pay()
c = employee()
c.name = 'Bilal'
c.id =12423432857
c.department = 'Software Engineer'
c.pay()