Constructor
1
A constructor is a special kind of method which is used for initializing the
instance variables during object creation.
Constructor is used for initializing the instance members when we create the
object of a class.
Constructors can be of two types.
Parameterized Constructor
Non-parameterized Constructor
Syntax:
def __init__(self):
#now all about the constructor
Default Constructor
2
class DemoClass:
num = 101
def read_number(self):
print(self.num)
obj = DemoClass()
obj.read_number()
Output:
101
Constructor without Parameter
3
class DemoClass:
num = 101
def __init__(self):
self.num = 999
def read_number(self):
print(self.num)
obj = DemoClass()
obj.read_number()
Output:
999
Constructor
4
class DemoClass:
num = 101
def __init__(self, data):
self.num = data
def read_number(self):
print(self.num)
obj = DemoClass(55)
obj.read_number()
obj2 = DemoClass(66)
obj2.read_number()
Output:
55
Constructor
5
class Addition:
num1=1000
num2=2000
num3=3000
def result(self):
self.num=self.num1+self.num2+self.num3
print('Output:',self.num)
Sum = Addition()
Sum.result()
Constructor
6
class Addition:
def __init__(self):
self.num1=1000
self.num2=2000
self.num3=3000
def result(self):
self.num=self.num1+self.num2+self.num3
print('Output:',self.num)
Sum = Addition()
Sum.result()
Constructor
7
class Person:
name = ""
age = 0
def __init__(self, personName, personAge):
self.name = personName
self.age = personAge
def showName(self):
print(self.name)
def showAge(self):
print(self.age)
person1 = Person("Richard", 23)
person2 = Person("Anne", 30)
person1.showAge()
person2.showName()
Variables
8
•Class or Static Variables
•shared by all objects
•Instance or non-static
•every object has a copy of it
•Global
• variable declared outside of the function or in
global scope is known as global variable.
•Local Variables
•variable declared inside the function's body or
in the local scope is known as local variable.
Class /static and instance/non static variables
9
class College:
branch = 'cse' # Class Variable
year = 2 # Class Variable
def __init__(self,name,roll):
self.name = name # Instance Variable
self.roll = roll # Instance Variable
a = College('abc', 1)
b = College('xyz', 2)
print(a.branch)
print(b.branch)
print(a.year)
print(b.year)
print(a.name)
print(b.name)
print(a.roll)
print(b.roll)
Global and local variables
10
def f():
print(s)
# Global scope
s = “It is better to fail in originality than to succeed in imitation"
f()
Output
It is better to fail in originality than to succeed in imitation
Global and local variables
11
def f():
s = "Stop chasing the money and start chasing the passion."
print(s)
# Global scope
s = "The way to get started is to quit talking and begin doing"
f()
print(s)
Output
Stop chasing the money and start chasing the passion
The way to get started is to quit talking and begin doing
Global and local variables
12
def f():
print(s)
s = "Stop chasing the money and start chasing the passion."
print(s)
# Global scope
s = "The way to get started is to quit talking and begin doing"
f()
print(s)
undefined: Error: local variable 's' referenced before assignment
Global and local variables
13
def f():
global s
print(s)
s = “I love programming"
print(s)
# Global Scope
s = "Python is great!"
f()
print(s)
Output
Python is great!
I love programming
I love programming
Global and local variables
14
def f():
y = "local"
f()
print(y)
NameError: name 'y' is not defined
Global and local variables
15
def f():
y = "local"
print(y)
f()
Output
local
Global and local variables
16
x = "global"
def f():
global x
y = "local"
x = x * 2
print(x)
print(y)
f()
Output
global global
local
Global and local variables
17
x = 5
def f():
x = 10
print("local x:", x)
f()
print("global x:", x)
Output
local x 10
global x 5

Constructor and variables

  • 1.
    Constructor 1 A constructor isa special kind of method which is used for initializing the instance variables during object creation. Constructor is used for initializing the instance members when we create the object of a class. Constructors can be of two types. Parameterized Constructor Non-parameterized Constructor Syntax: def __init__(self): #now all about the constructor
  • 2.
    Default Constructor 2 class DemoClass: num= 101 def read_number(self): print(self.num) obj = DemoClass() obj.read_number() Output: 101
  • 3.
    Constructor without Parameter 3 classDemoClass: num = 101 def __init__(self): self.num = 999 def read_number(self): print(self.num) obj = DemoClass() obj.read_number() Output: 999
  • 4.
    Constructor 4 class DemoClass: num =101 def __init__(self, data): self.num = data def read_number(self): print(self.num) obj = DemoClass(55) obj.read_number() obj2 = DemoClass(66) obj2.read_number() Output: 55
  • 5.
  • 6.
    Constructor 6 class Addition: def __init__(self): self.num1=1000 self.num2=2000 self.num3=3000 defresult(self): self.num=self.num1+self.num2+self.num3 print('Output:',self.num) Sum = Addition() Sum.result()
  • 7.
    Constructor 7 class Person: name ="" age = 0 def __init__(self, personName, personAge): self.name = personName self.age = personAge def showName(self): print(self.name) def showAge(self): print(self.age) person1 = Person("Richard", 23) person2 = Person("Anne", 30) person1.showAge() person2.showName()
  • 8.
    Variables 8 •Class or StaticVariables •shared by all objects •Instance or non-static •every object has a copy of it •Global • variable declared outside of the function or in global scope is known as global variable. •Local Variables •variable declared inside the function's body or in the local scope is known as local variable.
  • 9.
    Class /static andinstance/non static variables 9 class College: branch = 'cse' # Class Variable year = 2 # Class Variable def __init__(self,name,roll): self.name = name # Instance Variable self.roll = roll # Instance Variable a = College('abc', 1) b = College('xyz', 2) print(a.branch) print(b.branch) print(a.year) print(b.year) print(a.name) print(b.name) print(a.roll) print(b.roll)
  • 10.
    Global and localvariables 10 def f(): print(s) # Global scope s = “It is better to fail in originality than to succeed in imitation" f() Output It is better to fail in originality than to succeed in imitation
  • 11.
    Global and localvariables 11 def f(): s = "Stop chasing the money and start chasing the passion." print(s) # Global scope s = "The way to get started is to quit talking and begin doing" f() print(s) Output Stop chasing the money and start chasing the passion The way to get started is to quit talking and begin doing
  • 12.
    Global and localvariables 12 def f(): print(s) s = "Stop chasing the money and start chasing the passion." print(s) # Global scope s = "The way to get started is to quit talking and begin doing" f() print(s) undefined: Error: local variable 's' referenced before assignment
  • 13.
    Global and localvariables 13 def f(): global s print(s) s = “I love programming" print(s) # Global Scope s = "Python is great!" f() print(s) Output Python is great! I love programming I love programming
  • 14.
    Global and localvariables 14 def f(): y = "local" f() print(y) NameError: name 'y' is not defined
  • 15.
    Global and localvariables 15 def f(): y = "local" print(y) f() Output local
  • 16.
    Global and localvariables 16 x = "global" def f(): global x y = "local" x = x * 2 print(x) print(y) f() Output global global local
  • 17.
    Global and localvariables 17 x = 5 def f(): x = 10 print("local x:", x) f() print("global x:", x) Output local x 10 global x 5