Dynamic ways of creating variables in python

Dynamic ways of creating variables in python

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.

No alt text provided for this image




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.

  1. Using dictionary(This is probably the best approach): Dictionary will be the first thought for everyone. Adding these variables as keys of a dictionary and assigning it with required values as below

No alt text provided for this image


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

  • globals() built-in function will return global symbol table as dictionary. This will contain all the attributes,functions,classes ,imports(and any other objects), in global scope of respective module.

For example in scope of our scenario:

If you initialize num = 1 , then?variable 'num' will be stored as globals()['num']=1

  • locals() built-in function will return local symbol table with entities in local scope of a function or a class.

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. )

No alt text provided for this image

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.

  • Setting instance variables?dynamically from source_list at the time of instantiation of object

No alt text provided for this image

  • Setting instance variables using a setter method

No alt text provided for this image

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:

No alt text provided for this image


Rahul K

Test Lead | UST | Telecom | Capital Markets|Certified GCP Associate cloud engineer

2 年

Thanks for sharing

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了