1. Namespace
1
•Namespace is a collection of names
•A namespace represents a memory block where names are
•mapped (or linked) to objects.
2. Namespace
2
def outer_function():
a = 20
def inner_function():
a = 30
print('a =',a)
inner_function()
print('a =',a)
a = 10
outer_function()
print('a =',a)
Output
3. Namespace
3
def outer_function():
global a
a = 20
def inner_function():
global a
a = 30
print('a =',a)
inner_function()
print('a =',a)
a = 10
outer_function()
print('a =',a)
Output
4. Methods
4
There are three types of methods in Python:
•Instance Methods
•Static Methods
• Class Methods
5. Instance Methods
5
•Instance methods are the most common
type of methods in Python classes. These
are so called because they can access
unique data of their instance.
•Instance methods must have self as a
parameter
•No decorator
6. Instance Methods
6
class Inexample:
def __init__(self):
print('Hello')
self.name = 'champion'
def fun(self):
print('Have a nice day ')
print('My name is ' + self.name)
de = Inexample()
de.fun()
output
Hello
Have a nice day
My name is champion
7. Static Methods
7
•Static methods are methods that are related to a
class in some way, but don’t need to access any
class-specific data
•Don’t have to use self
•Don’t even need to instantiate an instance, simply
call method:
•The @staticmethod decorator is used to tell
python that this method is a static method.
8. Static Methods
8
class stexample:
def __init__(self):
print('Hello')
@staticmethod
def fun():
print(‘If you can dream it, you can do it.’)
de = stexample.fun()
output
If you can dream it, you can do it.
9. Class Methods
9
•Class methods know about their class. They can’t
access specific instance data, but they can call
other static methods.
•Class methods don’t need self as an argument, but
they do need a parameter called cls. This stands
for class
•Class methods are created using
the @classmethod decorator.
10. Class Methods
10
class clexample:
def __init__(self):
print('Hello')
@classmethod
def fun1(cls):
print('Im good student')
cls.fun2()
@staticmethod
def fun2():
print('Hello!')
de = clexample()
de.fun1()