0% found this document useful (0 votes)
3 views

python42

The document provides an overview of Python's object model, focusing on the concepts of objects, namespaces, and variable scopes. It explains the differences between built-in, global, and local namespaces, and illustrates how variables are defined and accessed within these scopes. Additionally, it covers how to modify global variables using the 'global' keyword and provides code examples to demonstrate these concepts.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

python42

The document provides an overview of Python's object model, focusing on the concepts of objects, namespaces, and variable scopes. It explains the differences between built-in, global, and local namespaces, and illustrates how variables are defined and accessed within these scopes. Additionally, it covers how to modify global variables using the 'global' keyword and provides code examples to demonstrate these concepts.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 1/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Cheat Sheet

Scope & Namespaces

Object

In general, anything that can be assigned to a variable in Python is referred


to as an object.

Strings, Integers, Floats, Lists, Functions, Module etc. are all objects.

Identity of an Object

Whenever an object is created in Python, it will be given a unique


identifier (id).This unique id can be different for each time you run the
program.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 2/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Every object that you use in a Python Program will be stored in Computer
Memory

The unique id will be related to the location where the object is stored in
the Computer Memory.

Name of an Object

Name or Identifier is simply a name given to an object.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 3/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Namespaces

A namespace is a collection of currently defined names along with


information about the object that the name references.

It ensures that names are unique and won’t lead to any conflict.

Namespaces allow us to have the same name referring different things in


different namespaces.

Code

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 4/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

PYTHON

1 def greet_1():
2 a = "Hello"
3 print(a)
4 print(id(a))
5
6 def greet_2():
7 a = "Hey"
8 print(a)
9 print(id(a))
10

Expand

Output

Namespace - 1
Hello
140639382368176
Namespace - 2
Hey
140639382570608

Types of namespaces

As Python executes a program, it creates namespaces as necessary and


forgets them when they are no longer needed.

Different namespaces are:

1. Built-in

2. Global

3. Local

Built-in Namespace

Created when we start executing a Python program and exists as long as the
program is running.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 5/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

This is the reason that built-in functions like id(), print() etc. are always
available to us from any part of the program.

Global Namespace

This namespace includes all names defined directly in a module (outside of


all functions).

It is created when the module is loaded, and it lasts until the program ends.

Local Namespace

Modules can have various

functions and classes .

A new local namespace is created when a function is called, which lasts until
the function returns.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 6/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Scope of a Name

The scope of a name is the region of a program in which that name has
meaning.

Python searches for a name from the inside out, looking in the

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 7/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

local , global , and finally the built-in namespaces.

Global variables

In Python, a variable defined outside of all functions is known as a global


variable.

This variable name will be part of Global Namespace.

Example 1

Code

PYTHON
1 x = "Global Variable"
2 print(x)
3
4 def foo():
5 print(x)
6
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 8/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave
6
7 foo()

Output

Global Variable
Global Variable

Example 2

Code
PYTHON

1 def foo():
2 print(x)
3
4 x = "Global Variable"
5
6 foo()

Output

Global Variable

Local Variables

In Python, a variable defined inside a function is a local variable.

This variable name will be part of the Local Namespace which will be
created when the function is called and lasts until the function returns.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=1456509… 9/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Code
PYTHON

1 def foo():
2 x = "Local Variable"
3 print(x)
4
5 foo()
6 print(x)

Output

Local Variable
NameError: name 'x' is not defined

As,

x is not declared before assignment, python throws an error.

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=145650… 10/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Local Import

Code
PYTHON

1 def foo():
2 import math
3 print(math.pi)
4
5 foo()
6 print(math.pi)

Output

3.141592653589793
NameError: name 'math' is not defined

Local Variables & Global Variables

Code
PYTHON

1 x = "Global Variable"
2
3 def foo():
4 x = "Local Variable"
5 print(x)
6
7 print(x)
8 foo()
9 print(x)

Output

Global Variable
Local Variable
Global Variable

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=145650… 11/12
7/27/24, 9:53 PM Revolutionizing the Job Market | NxtWave

Modifying Global Variables

global keyword is used to define a name to refer to the value in Global


Namespace.

Code
PYTHON

1 x = "Global Variable"
2
3 def foo():
4 global x
5 x = "Global Change"
6 print(x)
7
8 print(x)
9 foo()
10 print(x)

Output

Global Variable
Global Change
Global Change

https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=145650… 12/12

You might also like