Python Function Tutorial – Part X

Articles From: QuantInsti
Website: QuantInsti

See the previous installment in this series, Nested Python functions, to get up-to-date in this tutorial.

Variable Namespace and Scope

If we read the The Zen of Python (try import this in Python console), the last line states Namespaces are one honking great idea — let’s do more of those! Let’s try to understand what these mysterious namespaces are. However, before that, it will be worth spending some time understanding names in the context of Python.

Names in the Python world

name (also known as an identifier) is simply a name given to an object. From Python basics, we know that everything in Python are objects. And a name is a way to access the underlying object. Let us create a new variable with a name price having a value 144, and check the memory location identifier accessible by the Python function id.

# Creating new variable
price = 144

# Case 1: Print memory id of the variable price
print(id(price))

# Case 1: Output
1948155424

# Case 2: Print memory id of the absolute value 144
print(id(144))

# Case 2: Output
1948155424

Interestingly we see that the memory location of both cases (the variable and its assigned value) is the same. In other words, both refer to the same integer object. If you would execute the above code on your workstation, memory location would almost certainly be different, but it would be the same for both the variable and value. Let’s add more fun to it. Consider the following code:

# Assign price to old_price
old_price = price
# Assign new value to price
price = price + 1
# Print price
print(price)
# Output
145
# Print memory location of price and 145
print(‘Memory location of price:’, id(price))
print(‘Memory location of 145:’, id(145))
# Output
Memory location of price: 1948155456
Memory location of 145: 1948155456
# Print memory location of old_price and 144
print(‘Memory location of old_price:’, id(old_price))
print(‘Memory location of 144:’, id(144))
# Output
Memory location of old_price: 1948155424
Memory location of 144: 1948155424

We increased the value of a variable price by 1 unit and see that the memory location of it got changed. As you may have guessed, the memory location of an integer object 145 would also be the same as that of price. However, if we check the memory location of a variable old_price, it would point to the memory location of integer object 144. This is efficient as Python does not need to create duplicate objects. This also makes Python powerful in a sense that a name could refer to any object, even functions. Note that python functions are also objects in Python. Now that we are aware of the nitty-gritty of names in Python, we are ready to examine namespaces closely.

In the next installment, the author will discuss Namespace.

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.