Dynamic ways of creating variables in python
Akhil Pathirippilly mana
Data Engineering / Data Warehousing / Azure / AWS /CDP/Databricks
As a python developer or as a data engineer who uses python on their day to day work life , we may end up in a situation where we need to declare and initialize variables according to some input values .
?
For example here is the list of sources passed an argument to some python process (function or class or a module) by a user or some other python process. And this list can be vary at any time.
The requirement is for every source in source_list , we need to declare a variable in such a way that the variable name should be <source>_config.(One can achieve this requirement even without declaring variables dynamically , but for making the scenario simple, please bear with this example :) )
Let's see how can all we achieve this.
2. Using globals() built-in function (Symbol Table):Python interpreter maintains a data structure containing information about each identifier appearing in the program's source code. This information is about the type, value, scope level, and location of an identifier (also called symbol). We can call them as symbol tables
For example in scope of our scenario:
领英推荐
If you initialize num = 1 , then?variable 'num' will be stored as globals()['num']=1
For example in scope of our scenario:
If you initialize num = 1 globally and num=2 locally inside a function , global symbol table will store as previous example while local symbol table will store as locals()['num']=2. Whatever you declare or import with-in the scope of that function only will be stored on locals symbol table
Now let's come back to our original scenario of dynamically declaring variables. As we have seen above, global and local symbol tables are the places where variables of global and local scope are stored and retrieved. So why don't we directly access these data structures and add the variables directly to them in a dynamic fashion right? But there is a catch here that assigning new variables directly to locals() won't work inside a function as it works for global. So If you are planning to use this just to create local dynamic variable , this is not the method for you. But yes, globally you can do as follows (You need to make sure you are not overwriting any global variables which are already loaded by system or program unintentionally. )
3. Using setattr built-in method: setattr(obj, name, value) will call self.__setattr__ dunder method and will set value assigned to "name" argument as attribute name and value assigned to "value" argument as its original value. This can be utilized for dynamic assignment of instance attributes (This is a standard way of dealing with dynamic attributes if you are inside a class). This can be implemented either within __init__ or in a different setter method.
4. Using exec built-in function : This function is used for the dynamic execution of Python program which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed. We must be careful that the return statements may not be used outside of function definitions not even within the context of code passed to the exec() function. It doesn’t return any value, hence returns None. ???
Here in our scenario ,?if we need to assign salesforce_config="some config", we need to pass this entire statement as an argument to exec(). Please see the below code and output:
Test Lead | UST | Telecom | Capital Markets|Certified GCP Associate cloud engineer
2 年Thanks for sharing