Close Navigation
Learn more about IBKR accounts
Python Function Tutorial – Part XI

Python Function Tutorial – Part XI

Posted July 10, 2020
Jay Parmar
QuantInsti

See the previous installment in this series Variable Namespace and Scope, to get up-to-date in this tutorial.

Namespace

Name conflicts happen all the time in real life. For example, we often see that there are multiple students with the same name X in a classroom. If someone has to call the student X, there would be a conflicting situation for determining which student X is actually being called. While calling, one might use the last name along with the student’s first name to ensure that the call is made to the correct student X.

Similarly, such conflicts also arise in programming. It is easy and manageable to have unique names when programs are small without any external dependencies. Things start becoming complex when programs become larger and external modules are incorporated. It becomes difficult and wearisome to have unique names for all objects in the program when it spans hundreds of lines.

A namespace can be thought of a naming system to avoid ambiguity between names and ensures that all the names in a program are unique and can be used without any conflict. Most namespaces are implemented as a dictionary in Python. There is a name to object mapping, with names as keys and objects as values. Multiple namespaces can use the same name and map it to a different object. Namespaces are created at different moments and have different lifetimes. Examples of namespaces are:

  • The set of built-in names: It includes built-in functions and built-in exception names.
  • The global names in a module: It includes names from various modules imported in a program.
  • The local names in a function: It includes names inside a function. It is created when a python function is called and lasts until the function returns.

The important thing to know about namespaces is that there is absolutely no relation between names in different namespaces; that is, two different modules can contain a function sum without any conflict or confusion. However, they must be prefixed with the module name when used.

Scopes

Until now we’ve been using objects anywhere in a program. However, an important thing to note is not all objects are always accessible everywhere in a program. This is where the concept of scope comes into the picture. A scope is a region of a Python program where a namespace is directly accessible. That is when a reference to a name (lists, tuples, variables, etc.) is made, Python attempts to find the name in the namespace. The different types of scopes are:

Local scope: Names that are defined within a local scope means they are defined inside a python function. They are accessible only within a function. Names defined within a function cannot be accessed outside of it. Once the execution of a function is over, names within the local scope cease to exist. This is illustrated below:

# Defining a function
def print_number():
# This is local scope
n = 10
# Printing number
print(‘Within function: Number is’, n)
print_number()

# This statement will cause error when executed
print(‘Outside function: Number is’, n)

# Output
Within function: Number is 10
Traceback (most recent call last):
File ““, line 8, in
print(‘Outside function: Number is’, n)
NameError: name ‘n’ is not defined

In the next installment, the author will discuss Enclosing scope.

Visit https://www.quantinsti.com/ for ready-to-use Python functions as applied in trading and data analysis.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.