Back to Community
Why is context.CONSTANT preferred vs simply defining a global 'CONSTANT'

A question for Q but also the community in general.

Why is context.CONSTANT preferred vs simply defining 'CONSTANT' at a global level.

import pandas as pd

CONSTANT = 20 # don't use this syntax

def initialize(context):  
   context.CONSTANT = 20  # use this instead

This was a recommendation in this post from 2015 . Is this still preferred and why.

Thanks in advance.

4 responses

Thanks Dan. At this point, this isn't a hard and fast rule by which you need to abide. Nothing currently in the system will break if you do so. That said, we can envision future improvements to the platform that might make it important for you to have stored any state or important variables in context.

TL;DR: not a big deal right now; might possibly be in the future


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

In general global scope is frowned upon so you don't have collisions with variables in other methods/functions that in this case don't have access to context. It would be especially dangerous if your project were to incorporate libraries/classes from other developers who also used global scope -- so that's why it's considered best practice to avoid altogether.

The way context is used in Q though you can run into the same problem.

Thank you for both the responses. All makes sense. Much appreciated.

I'd been confused about the best practice, as well. In the Optimize API example (see, the algo parameters are globals, which seems like a bad practice, since there is no way to tell if one is stomping on globals elsewhere. Is there actually a risk? Or does the Quantopian API somehow sandbox the globals, so that they aren't actually true globals, but would only be available to code that the user has visible to him in the API?

Presumably, using context would be preferred, but since the optimize API example uses globals, maybe there is no risk?

Also, shouldn't settings be defined as constants (not sure Python supports this), so that they can't be changed?