In this article I'd like to describe the difference between three built-in methods: globals(), locals() and vars() (original link). Also I'd like to show some examples of their usage. So, globals() always returns the dictionary of the module namespace locals() always returns a dictionary of the current namespace vars() returns either a dictionary of the current namespace (if called with no argument) or the dictionary of the argument Let's see an example of theese functions in different namespaces. I have the module with following content: class A(): def __init__(self, id): = id print "Class locals:\t%s" ...

1. Create a new variable Now let's see the usage of these functions. The first one and the most interesting is defining of a new variable: >>> globals()['new_var'] = 1 >>> new_var 1 2. Searching for needed form And here is another example from Django framework which I used. Let's assume that we have a page with a few objects. And we want to add or edit objects on the same page. For example, we use pop-up window. We want to request needed form from view. We have classes for them and models that describes these objects. Each button on page will send the model name, which object we want to add/edit. So, for example we have models: ...
Published ago by AnaPana

Web development
Review Linux
Provisioning CVS
Windows AWS
Continuous Integration
NoSql Logging Web Servers FileSystems Scala
accurev ansible apach2 apache api application approaches architecture archivation argparse arguments artifactory automation awk aws backup bash batch-scripting beaver block build built-in caching call captcha cartridge case certificates cgi chain client cloud cloudformation cluster cmd coding collections command-line commands compression conditionals conversion convert cookbook copy counter cron crud css cut cygwin daemon datatype date dd debpackage decorator delattr deploy deque dict distributed-file-systems django DNS domain driver dropbox dump elasticcache elasticsearch encryption exit ext4 extra-tags failover file filename filters for form format freetds functions gerrit getattribute gid git globals glusterfs hardware hook iam indexing inheritance init install job-interview jquery ldap linux list locals logging logs logstash lookup magicmethods mail main-menu metadata metalogger mongodb moosefs mount mssql multiplatform multithreading mysql netcat nginx nosql open-ssh openldap openshift os packaging parse partition path pattern patterns permissions pid pil pip pipe playbook pool post post-commit processes production provisioning proxy putty python python-mysql recursively redirection redis register replication repr restore return review rotation scala script search selenium server setattr settings setup shipper signals singleton slots snapshot socket splunk ssh ssh-key ssl storage str string style subprocess sugar super switch syntactic syntaxhighlighter systeminfo tail tar templatetags time tls tune2fs tuple ubuntu unicode unique unix unixodbc usage usecases uuid uwsgi variable vars version vi virtualenv volume web web-server windows with_items __getattr__