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
A 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
# Creating new variable
price = 144
# Case 1: Print memory id of the variable price
# Case 1: Output
# Case 2: Print memory id of the absolute value 144
# Case 2: Output
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 memory location of price and 145
print(‘Memory location of price:’, id(price))
print(‘Memory location of 145:’, id(145))
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))
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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.