Creating class in python without using class keyword

add2 = lambda x,y: x+y
sub2 = lambda x,y: x-y
mul2 = lambda x,y: x*y
# Second parameter is tuple, used for inheriting classes
# Third parameter is dict, used for defining variables and functions
myclass = type('myclass', (), {'var1': 10, 'add': add2, 'sub': sub2, 'mul': mul2})
myinstance = myclass()
print(myinstance.add(10,20))
print(myinstance.sub(10,20))
print(myinstance.mul(10,20))

Comments

Pages

Categories

Tags