Python Variables

Following up on my previous post, in this one I will writing mostly on variables, scopes, modules


  • Variables are name, not containers
    • Everything is an object
    • Everything is a reference
    • Variables are neither
    • They are not allocated memory that holds data, they are neither objects or references they are only labels
  • Everything concrete is an object
    • Whenever you hold an object, whenever you store it anywhere you are storing a reference, you dont own an object, you just own their reference.
  • Everything that holds anything, holds references
  • Variables refers to objects
    • namespaces maps names to objects


  • 3 Scope rule
    • names are either local or global or built ins
  • Local names defined by assignment unless ‘global’ definition says otherwise
    • local means existing in current class, function, global means existing in current module and not existing everywhere
    • builtins is just a special namespace used by builtin functions
    • if you have a global of the same name as a builtin the compiler will find the global instead of the builtin


  • They have their own namespace, keeps the rest of the modules tidy
  • Executed on first import, namespace becomes attributes
  • Then cached in sys.modules
  • import is syntactic sugar, it calls in the following function
    • mod = __import__(“mod”)
  • sys.modules doesn’t have to contain only modules
    • it is just a mapping of names to whatever object you want import to return
  • If you map None to a name then it will raise import error

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s