0% found this document useful (0 votes)
13 views19 pages

BKclassANDobject 051

The document discusses classes and objects in Python. It shows how to define classes with methods and attributes. Key points covered include: - Defining classes with the class keyword and initializing objects of that class. - Using self to refer to instance attributes and methods from within methods. - Calling methods on objects by using dot notation (object.method()). - Passing arguments to methods during a method call. - Defining __init__() to set up instance attributes during object initialization. - Accessing attributes using dot notation on the self object (self.attribute).

Uploaded by

531Binod Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

BKclassANDobject 051

The document discusses classes and objects in Python. It shows how to define classes with methods and attributes. Key points covered include: - Defining classes with the class keyword and initializing objects of that class. - Using self to refer to instance attributes and methods from within methods. - Calling methods on objects by using dot notation (object.method()). - Passing arguments to methods during a method call. - Defining __init__() to set up instance attributes during object initialization. - Accessing attributes using dot notation on the self object (self.attribute).

Uploaded by

531Binod Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

In [1]:

class intern: #no error ,print empty ,


pass

In [2]:
class A:
print("Hello")

Hello

In [3]:
class B:
print("Hello")

obj=B() #obj is object which is a blue print of class B, B() is object constru
ctor
Hello

In [4]:
class B: #gives empty
x="Hello"
p=B()

In [5]:
class B: #gives empty
x="Hello"
p=B()

p.B()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-d35bb12a8451> in <module>
3 p=B()
4
----> 5 p.B()

AttributeError: 'B' object has no attribute 'B'

In [1]:
class B: #gives empty
x="Hello"
p=B()

print(p.B())

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-bf0a29743029> in <module>
3 p=B()
4
----> 5 print(p.B())

AttributeError: 'B' object has no attribute 'B'

In [2]:
class B: #gives empty
x="Hello"
p=B()
print(p.B(x))

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-f5c6a29d2546> in <module>
3 p=B()
4
----> 5 print(p.B(x))

AttributeError: 'B' object has no attribute 'B'

In [4]:
class B: #gives empty
x="Hello"
p=B()

print(p.x)

Hello

In [6]:
class B: # B is class and b,c are two object
name="Binod"
age=20
def f1():
print("go") #stil no output because f1 should be call
b=B()
c=B()

In [7]:
class B: # B is class and b,c are two object
name="Binod"
age=20
def f1():
print("go") #stil no output because in class f1 should be call through objec
t
b=B()
c=B()
f1()

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-826e0537cb8f> in <module>
6 b=B()
7 c=B()
----> 8 f1()

NameError: name 'f1' is not defined

In [8]:
class B: # B is class and b,c are two object
name="Binod"
age=20
def f1():
print("go") #stil no output because def f1 has zero arguments
b=B()
c=B()
b.f1() #here one arguments is b
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-ffd60d18f180> in <module>
6 b=B()
7 c=B()
----> 8 b.f1() #here one arguments is b

TypeError: f1() takes 0 positional arguments but 1 was given


In [9]:

class B: # B is class and b,c are two object


name="Binod" #class attributes
age=20 #class attributes
def f1(self):
print("go")
b=B()
c=B()
b.f1() #here one arguments is b
go

In [10]:
class B: # B is class and b,c are two object
name="Binod" #class attributes
age=20 #class attributes
def f1(self):
print("go")
b=B()
c=B()
b.f1() #here one arguments is b
c.f1()

go
go

In [11]:
class B: # B is class and b,c are two object
name="Binod" #class attributes
age=20 #class attributes
def f1(self):
print("go")
b=B()
c=B()
b.f1() #here one arguments is b
c.f1()
b.f1()

go
go
go

In [12]:
class B:
name="Binod"
age=20
def f1(s):
s=b+c
b=B(3) #bcz there is more than one arg b and 3.,so it internally call init function
c=B(5)
b.f1()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-3521139cc1f0> in <module>
4 def f1(s):
5 s=b+c
----> 6 b=B(3) #bcz there is more than one arg b and 3.,so it internally call init fu
nction
7 c=B(5)
8 b.f1()

TypeError: B() takes no arguments

In [13]:
class B:
name="Binod"
age=20
def __init__(self,k):
self.k=k
def f1(self):
print("qwet")
b=B(3)
c=B(5)
b.f1()

qwet

In [14]:
class B:
name="Binod"
age=20
def __init__(self,k):
self.k=k
print(k.b+k.c) #b.k+c.k ,always object.something
def f1(self):
print("qwet")
b=B(3)
c=B(5)
b.f1()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-0e744e1c43af> in <module>
7 def f1(self):
8 print("qwet")
----> 9 b=B(3)
10 c=B(5)
11 b.f1()

<ipython-input-14-0e744e1c43af> in __init__(self, k)
4 def __init__(self,k):
5 self.k=k
----> 6 print(k.b+k.c) #b.k+c.k ,always object.something
7 def f1(self):
8 print("qwet")

AttributeError: 'int' object has no attribute 'b'

In [3]:
class B:
name="Binod"
age=20
def __init__(self,k):
self.k=k
print(b.k+c.k)
def f1(self):
print("qwet")
b=B(3)
c=B(5)
b.f1()

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-d8cf47470195> in <module>
7 def f1(self):
8 print("qwet")
----> 9 b=B(3)
10 c=B(5)
11 b.f1()

<ipython-input-3-d8cf47470195> in __init__(self, k)
4 def __init__(self,k):
5 self.k=k
----> 6 print(b.k+c.k)
7 def f1(self):
8 print("qwet")

NameError: name 'b' is not defined

In [16]:
class B: # B is class and b,c are two object
name="Binod" #class attributes
age=20 #class attributes
def __init__(self,a):
self.a=a
print(b+c)
def f1(self):
print("qwe")
b=B(3) #here b and 3 are two arguments ,it internally call init funtion which has onl
y one arg.so error
c=B(5)
b.f1()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-e0f4cd44f1cf> in <module>
7 def f1(self):
8 print("qwe")
----> 9 b=B(3) #here b and 3 are two arguments ,it internally call init funtion which
has only one arg.so error
10 c=B(5)
11 b.f1()

<ipython-input-16-e0f4cd44f1cf> in __init__(self, a)
4 def __init__(self,a):
5 self.a=a
----> 6 print(b+c)
7 def f1(self):
8 print("qwe")

TypeError: unsupported operand type(s) for +: 'B' and 'B'

In [ ]:

In [17]:
def f1(): #no out put bcz cll the function
print("qwe")

In [18]:
def f1(): #no out put bcz cll the function out of block of code
print("qwe")
f1()

In [19]:

def f1(): # gives output but in class f1 should be call through object
print("qwe")
f1()

qwe

In [20]:
class Test:
def sum(self,a,b):
s=a+b
return s
a=int(input("Enter the 1st no. : "))
b=int(input("Enter the 2nd no. " ))
obj=Test()
print("sum is" ,s)
Enter the 1st no. : 23
Enter the 2nd no. 23

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-20-c4b2297093cf> in <module>
6 b=int(input("Enter the 2nd no. " ))
7 obj=Test()
----> 8 print("sum is" ,s)

NameError: name 's' is not defined

In [21]:
class Test:
def sum(self,a,b):
s=a+b
return s
a=int(input("Enter the 1st no. : "))
b=int(input("Enter the 2nd no. " ))
obj=Test()
s=obj.sum()
print("sum is" ,s)

Enter the 1st no. : 2


Enter the 2nd no. 3

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-ea76e2c015eb> in <module>
6 b=int(input("Enter the 2nd no. " ))
7 obj=Test()
----> 8 s=obj.sum()
9 print("sum is" ,s)

TypeError: sum() missing 2 required positional arguments: 'a' and 'b'

In [22]:
class Test:
def sum(self,a,b):
s=a+b
return s
a=int(input("Enter the 1st no. : "))
b=int(input("Enter the 2nd no. " ))
obj=Test()
s=obj.sum(a,b) #we can give arg in fn call
print("sum is" ,s)

Enter the 1st no. : 4


Enter the 2nd no. 5
sum is 9

In [23]:
class Test:
def sum(self,a,b):
s=a+b
return s
a=int(input("Enter the 1st no. : "))
b=int(input("Enter the 2nd no. " ))
obj=Test() #there is one arguments i.e obj so no need init function
obj.sum(a,b)

Enter the 1st no. : 6


Enter the 2nd no. 1
Out[23]:
7

In [24]:
class Sum:
def __init__(self,a,b):
self.a=a
self.b=b
print(a1.a+a2.a)

a1=Sum(30,20)
a2=Sum(10,5)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-24-af888706d47c> in <module>
8
9
---> 10 a1=Sum(30 ,20 )
11 a2=Sum(10 ,5)
12

<ipython-input-24-af888706d47c> in __init__(self, a, b)
3 self.a=a
4 self.b=b
----> 5 print(a1.a+a2.a)
6
7

NameError: name 'a1' is not defined

In [27]:
class Sum: #no output ,must call thrfunction w.r.t object
def __init__(self,a,b):
self.a=a
self.b=b
def avg(self):
print((self.a+self.b)/2)

a1=Sum(30,20)
a2=Sum(10,5)

In [28]:
class Sum:
def __init__(self,a,b): #self=a1 or a2 ,a=30 or 10 ,b=20 or 5
self.a=a #self.a=a=30 when a1 is call otherwise 10(from above)
self.b=b #self.b=b=20
def avg(self):
print((self.a+self.b)/2) #(30+20)/2

a1=Sum(30,20)
a2=Sum(10,5)
a1.avg()
a2.avg()

25.0
7.5

In [38]:
class Sum:
name=("Binod")
def __init__(self,a,b):
self.a=a
self.b=b
def avg(self): #self is one arg. bcz when we call fn by using obj ,there
will be one object arg.
print((self.a+self.b)/2)

a1.name #no executed


a1=Sum(30,20)
a2=Sum(10,5)
a1.avg()
a2.avg()
print(a1.name)
print(a2.name)
a1.name

25.0
7.5
Binod
Binod
Out[38]:
'Binod'

In [35]:
class Sum:
name=("Binod")
def __init__(self,a,b):
self.a=a
self.b=b
def avg(self): #self is one arg. bcz when we call fn by using obj ,there
will be one object arg.
print((self.a+self.b)/2)
def intrn(cls): #cls is any arg for fn callthrough obj
print(cls.name)

a1=Sum(30,20)
a2=Sum(10,5)
a1.avg()
a2.avg()
print(a1.name)
print(a2.name)
a1.name
a1.intrn()
a2.intrn()

25.0
7.5
Binod
Binod
Binod
Binod

In [36]:
class Sum:
name=("Binod")
def __init__(self,a,b):
self.a=a
self.b=b
def avg(self): #self is one arg. bcz when we call fn by using obj ,there
will be one object arg.
print((self.a+self.b)/2)
def intrn(cls): #cls is any arg for fn callthrough obj
print(cls.name)

a1=Sum(30,20)
a2=Sum(10,5)
a1.avg()
a2.avg()
print(a1.name)
print(a2.name)
#a1.name
a1.intrn()
a2.intrn()

25.0
7.5
Binod
Binod
Binod
Binod

In [39]:
class Sum:
name=("Binod")
def __init__(self,a,b):
self.a=a
self.b=b
def avg(self): #self is one arg. bcz when we call fn by using obj ,there
will be one object arg.
print((self.a+self.b)/2)
def intrn(cls): #cls is any arg for fn callthrough obj
print(cls.name)

a1=Sum(30,20)
a2=Sum(10,5)
a1.avg()
a2.avg()
print(a1.name)
print(a2.name)
#a1.name
a1.intrn()
a2.intrn()
Sum.intrn(a1) #it will be executed, class name.fn(obj)
25.0
7.5
Binod
Binod
Binod
Binod
Binod

In [5]:
class Sum:
name=("Binod")
def __init__(self,a,b): #self=a1 ,a=30 ,b=20 when fn is call through obj a1.
self.a=a
self.b=b
def __add__(self,o): #self=a1 and o=a2 .there is two arg bcz we have to access tw
o values from two obj
k=self.a + o.a
l=self.a + o.b
m=self.b + o.a
n=self.b + o.b
print(k,l,m,n)

a1=Sum(30,20)
a2=Sum(10,5)

In [1]:
class Sum:
name=("Binod")
def __init__(self,a,b): #self=a1 ,a=30 ,b=20 when fn is call through obj a1.
self.a=a
self.b=b
def __add__(self,o): #self=a1 and o=a2 .there is two arg bcz we have to access tw
o values from two obj
k=self.a + o.a #k=30+10 ,where self means in a1 and o means in a2
l=self.a + o.b
m=self.b + o.a
n=self.b + o.b
print(k,l,m,n)

a1=Sum(30,20)
a2=Sum(10,5)
a3=a1+a2

40 35 30 25

local and global scope


In [7]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
print(b) #global
print(a) #global
f1() # local variable (f1 call last ,so f1 execute last)
90
12
local 10

In [9]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
f1()
print(b) #global
print(a) #global
# local variable (f1 call 1st ,so f1 execute lst)
local 10
90
12

In [8]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
print(b) #global
print(a) #global
# f1 not executed bcz not f1 call till now
90
12

In [33]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"]
print(a)
f1()

12
local 10

In [34]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"] # globals , not global
print(x)
print(a)
f1()

12
local 10
12

In [35]:
b=90
c=50
a=12
print(id(a))
def f1():
a=10
print("local",a)
x=globals()["a"] #same id ie using globals key we can access global value ins
ide a function
print(id(x))
print(a)
f1()

140728822474896
12
local 10
140728822474896

In [36]:

b=90
c=50
a=12
print(id(a))
def f1():
a=10
print("local",a)
x=globals()["a"]=15 #now id will be diff bcz value is diff
print(id(x))
print(a)
f1()

140728822474896
12
local 10
140728822474992

In [38]:

b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"]=15 #global variable is 15
print(x)
print(a)
f1()

12
local 10
15

In [10]:

b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"]=15 #global variable is 15
print(x)

f1() #1stly f1 call means value has been changed


print(a)

local 10
15
15

In [39]:

b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"]=15 #global variable is 15

print(a)
f1()

12
local 10

In [40]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"]=15 #global variable is 15

print(a)
f1()
print(a) #print global value of a after change inside a function ,so 1st f1 should be cal
l, then it got changes
12
local 10
15

In [41]:
b=90
c=50
a=12
def f1():
a=10
print("local",a)
x=globals()["a"]=15 #global variable is 15
print(x) #print 15
print(a)
f1()
print(a) #print 15
12
local 10
15
15

inheritance
In [44]:

class A:
def f1(self): #each class must has its obj for execution
print("f1")
class B:
def f2(self): # must be one arg for callin f2
print("f2")
a=A() #here is only one arg so no init fn
b=B()
a.f1()
b.f2() #to print anything ,there must be fullfill class,object,constructor,fn call ,
actual arg=formal arg
f1
f2

In [45]:
class A:
def f1(self): #each class must has its obj for execution
print("f1")
class B:
def __init__(self,a,b):
print("init")
def f2(self): # must be one arg for callin f2
print("f2")
a=A(3,7) #here is 3 arg(more than one) so init fn is complsary
b=B(8,9)
a.f1()
b.f2()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-270870844747> in <module>
7 def f2(self): # must be one arg for callin f2
8 print("f2")
----> 9 a=A(3,7) #here is 3 arg(more than one) so init fn is complsary
10 b=B(8,9)
11 a.f1()

TypeError: A() takes no arguments

In [46]:

class A:
def __init__(self,a,b): #1st of all, init fn executed without call
print("init A")
def f1(self): #each class must has its obj for execution
print("f1")
class B:
def __init__(self,a,b):
print("init")
def f2(self): # must be one arg for callin f2
print("f2")
a=A(3,7) #here is 3 arg(more than one) so init fn is complsary
b=B(8,9)
a.f1()
b.f2()

init A
init
f1
f1
f2

In [47]:
class A:
def __init__(self,p,q): # we can take any other than p,q
print("init A")
def f1(self): #each class must has its obj for execution
print("f1")
class B:
def __init__(self,a,b): # or other than a,b or same obj name
print("init")
def f2(self): # must be one arg for callin f2
print("f2")
a=A(3,7) #here is 3 arg(more than one) so init fn is complsary
b=B(8,9)
a.f1()
b.f2()

init A
init
f1
f2

In [49]:

#creating two parent class A and B


class A:
def f1(self):
print("f1")
class B:
def f2(self):
print("f2")
a=A()
b=B()
a.f1()
b.f2()
b.f1() #f1 is inside class A it gives error ,so only f1 and f2 will be print

f1
f2

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-49-3dd11d947aba> in <module>
9 a.f1()
10 b.f2()
---> 11 b.f1() #f1 is inside class A it gives error ,so only f1 and f2 will be print

AttributeError: 'B' object has no attribute 'f1'

In [50]:

#creating one parent class A and chil class B , it is called single inheritence
class A: #parrent/base class
def f1(self):
print("f1")
class B(A): # B is child/derive class
def f2(self):
print("f2")
a=A()
b=B()
a.f1()
b.f2()
b.f1() #f1 is inside class A but B is child class of A,so B inherits all properties from
parents
f1
f2
f1
In [53]:
#creating 3 class, in which A is parrent class of B , and B is parent class of C. it is c
alled multilevel inheritence
class A: #parrent/base class of B
def f1(self):
print("f1")
class B(A): # B is child/derive class
def f2(self):
print("f2")
class C(B): #child of B
def f3(self):
print("f3")
a=A()
b=B()
c=C()
a.f1()
b.f2()
b.f1()
c.f3()
c.f2()
c.f1()

f1
f2
f1
f3
f2
f1

In [54]:
#creating 3 class, in which A is parrent class of Both A and B ,it is called Hierarchical
inheritence
class A: #parrent/base class of B
def f1(self):
print("f1")
class B(A): # child/derive class of A
def f2(self):
print("f2")
class C(A): #child of A
def f3(self):
print("f3")
a=A()
b=B()
c=C()
a.f1()
b.f2()
b.f1()
c.f3()
#c.f2 we can't access
c.f1()

f1
f2
f1
f3
f1

In [55]:

#creating class, in which A and B are parrent class of C ,it is called multiple inherite
nce
class A: #parrent/base class of B
def f1(self):
print("f1")
class B: # child/derive class of A
def f2(self):
print("f2")
class C(A,B): #child of A and B
def f3(self):
print("f3")
a=A()
b=B()
c=C()
a.f1()
b.f2()
c.f3()
c.f1()
c.f2()

f1
f2
f3
f1
f2

In [56]:

#creating class, in which there is more than one parent class and more than one child cl
ass ,
#it is called Hybride inheritence

class A: #parrent/base class of B


def f1(self):
print("f1")
class B(A): # child/derive class of A
def f2(self):
print("f2")
class C(A): #child of A and B
def f3(self):
print("f3")
class D(B,C):
def f4(self):
print("f4")
a=A()
b=B()
c=C()
d=D()
a.f1()
b.f2()
b.f1()
c.f3()
c.f1()
d.f4()
d.f2()
d.f1()
d.f3()
d.f1()

f1
f2
f1
f3
f1
f4
f2
f1
f3
f1

constructor in inheritance
In [57]:
class A:
def __init__(self):
print("init")
def f1():
print("f1 A")
class B:
def __init__(self):
print("init B")
def f2():
print("f2 B")
a=A()
b=B()

init
init B

In [58]:

class A:
def __init__(self):
print("init")
def f1():
print("f1 A")
class B(A): # init does not inherited by parent
def __init__(self):
print("init B")
def f2():
print("f2 B")
a=A()
b=B()

init
init B

In [60]:

class A:
def __init__(self):
print("init")
def f1(self):
print("f1 A")
class B(A): # init does not inherited by parent
def __init__(self):
print("init B")
def f2(self):
print("f2 B")
a=A()
b=B()
a.f1()
b.f2()
b.f1()

init
init B
f1 A
f2 B
f1 A

In [64]:

class A:
def __init__(self):
print("init")
def f1(self):
print("f1 A")
class B(A): # init does not inherited by parent
def __init__(self):
super().__init__() # now init is inheritd from parent don' missing dot, no col
on
print("init B")
def f2(self):
print("f2 B")
a=A()
b=B()
a.f1()
b.f2()
b.f1()

init
init
init B
f1 A
f2 B
f1 A

In [71]:
class A:
def __init__(self):
print("init")
def f1(self):
print("f1 A")
class B(A): # init does not inherited by parent
def __init__(self):
super().__init__()
print("init B")
super().f1() #f1 will be called by using this method
def f2():
print("f2 B")
a=A()
b=B()

init
init
init B
f1 A

In [73]:

class A:
def __init__(self):
print("init")
def f1(self):
print("f1 A")
class B: # init does not inherited by parent
def __init__(self):
print("init B")
def f2():
print("f2 B")
class C(A,B): #init is inherited from only one out of (A,B) which is left in (A,B)
i.e A
def __init__(self):
print("init c")
super().__init__()
a=A()
b=B()
c=C()

init
init B
init c
init

In [74]:
class A:
def __init__(self):
print("init")
def f1(self):
print("f1 A")
class B: # init does not inherited by parent
def __init__(self):
print("init B")
def f2():
print("f2 B")
class C(B,A): #init is inherited from only one out of (A,B) which is left in (B,A)
i.e B
def __init__(self):
print("init c")
super().__init__()
a=A()
b=B()
c=C()

init
init B
init c
init B

In [ ]:

You might also like