In [4]: class computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print(f"Selling Price : {self.__maxprice}")
def setmaxprice(self,price):
self.__maxprice = price
c = computer()
c.sell()
c.__maxprice = 1000
c.sell()
c.setmaxprice(1000)
c.sell()
Selling Price : 900
Selling Price : 900
Selling Price : 1000
In [5]: class computer:
def __init__(self):
self.maxprice = 900
def sell(self):
print(f"Selling Price : {self.maxprice}")
def setmaxprice(self,price):
self.maxprice = price
c = computer()
c.sell()
c.maxprice = 1000
c.sell()
c.setmaxprice(1000)
c.sell()
Selling Price : 900
Selling Price : 1000
Selling Price : 1000
In [9]: class Polygon:
def render(self):
print("Rendering Polygon...")
class Square(Polygon):
def render(self):
print("Rendering Square...")
class Circle(Polygon):
def render(self):
print("Rendering Circle...")
s1 = Square()
s1.render()
c1 = Circle()
c1.render()
Rendering Square...
Rendering Circle...
In [11]: class Person:
def __init__(self,name,age):
self.name = name
self.age = age
class Professor(Person):
def isProfessor(self):
return f"{self.name} is a Professor"
sir = Professor("Vipeen",40)
print(sir.isProfessor())
Vipeen is a Professor
In [12]: from os import name
class Animal:
name = ""
def eat(self):
print("I can eat")
class Dog(Animal):
def display(self):
print("My name is ",self.name)
def eat(self): #Overwrtting
print("I like to eat bones")
d1 = Dog()
d1.name = "Tommy"
d1.display()
d1.eat()
My name is Tommy
I like to eat bones
In [15]: class SubClass1:
n = 3
class SubClass2:
n2 = 5
class SubClass3(SubClass1,SubClass2):
def addition(self):
return self.n + self.n2
obj = SubClass3()
print(obj.addition())
In [17]: class Parent:
str1 = "VJTI"
class Child(Parent):
str2 = "VIPEEN"
obj = Child()
print(obj.str1,obj.str2)
VJTI VIPEEN
In [21]: class SuperClass:
x = 3
class SubClass(SuperClass):
pass
class SubClass2(SubClass):
pass
obj = SuperClass()
y = SubClass()
z = SubClass2()
print(obj.x)
print(y.x)
print(z.x)
3
3
3
In [26]: class one:
def display(self):
print("parent class")
class two:
def display(self):
print("sec parent class")
class three(two):
def display(self):
print("child class one")
class four(one,three):
def f4(self):
print("child class two")
In [24]: class x:
num = 10
class a(x):
pass
class b(x):
pass
class c(a):
pass
class d(a):
pass
obj = d()
print(obj.num)
10
This notebook was converted to PDF with convert.ploomber.io