Hey finally my very first article on python iterators got published on SDjournal.org magazine http://sdjournal.org/python-starterkit-released/.
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
I will add a series of these posts which are just a bunch of notes that I am going to create for learning Python in depth from one of the Google talks on Python.
- Everything is run time* (compile time* too is runtime)
- somethings are cached (.pyc files)
- Execution happens in namespace
- modules, functions and classes all have their own namespace.
- Modules are executed top to bottom
- just like a script
- def and class statements are runtime
- def statement creates a function
- Code is compiled at compile time
- into a code object
- The code for the function will be compiled into a code object, which is a separate object from whatever code the rest of the program is compiled into
- Wrapped in a function object at def time
- When the def statement is executed the code object is wrapped into a function object along with whatever arguments it takes and any defaults to those arguments
- Arguments defaults evaluated then too
- def is primarily an assignment statement
- essentially if you look at the bytecode then it is the same bytecode as the assignment operation
def func(arg1, arg2=func2()): print "entering func1" def inner_func(arg3=arg2): print "entering inner_func" return arg1, arg3 arg1 = arg2 = None return inner_func
When the def statement is reached that whole line gets evaluated including the default arguments and the rest of the program which is
print "entering func1" def inner_func(arg3=arg2): print "entering inner_func" return arg1, arg3 arg1 = arg2 = None return inner_func
has been compiled because the compile time is in the past but is not being executed until you call the function. When func() is executed the inner func is evaluated till the def statement along with the defaults but the rest is compiled and also the defaults are evaluated at this time so whatever arg2 was is assigned to arg3 and not arg3 will not get that value of arg2 when you call inner_func so if the value of arg2 changes later on the value of arg3 will still be that value of arg2 when the def statement was reached.
Pretty similar to functions but a little different as well
- Class statements executes suite
- in a separate namespace
- Class statement is followed by a block of code that is executed when the class statement is reached.
- Class object is created out of the dict
- name space becomes attribute space
- Methods are just functions inside a class
- Special wrappings happens automatically at attribute level
- self is passed implicitly but received explicitly
- Class body is normal code block.
* There is a pretty clear answer on stackoverflow regarding this concept which I would like to add here.
Run time and compile time are different stages in the running of a computer program. It is quite a difficult concept to learn and it is best approachable if you ask it in terms of these questions.
- What invariants the program satisfy.
- What can go wrong in this phase.
- If the phase succeeds what are the post conditions.
- What are inputs and outputs if any.
- The program need not satisfy any invariant, in fact it need not be well formed at all.
- Things that can go wrong here are
- Syntax errors
- Type checking errors
- Compiler crashes
- If the compiler succeeds we know that the program is well formed and it is possible to run the program.
- Inputs and Outputs?
- Inputs are the program itself along with the headers or any other libraries to be imported
- Output is the assembly code or relocatable object code or executable. If the program has errors then outputs are the bunch of those error messages.
- We know nothing of program invariants, they can be anything that the programmer specifies
- Things that can go wrong here are
- Division by zero
- Dereferencing a null pointer
- opening a file which is not already there
- If run time succeeds the program finished without crashing
- Inputs and outputs are entirely on the programmer.
These days a lot of people have to relocate to a different state in India because of work and they tend to take their vehicles with them. Traffic sleuths are always looking for a out of state vehicles to slap a huge fine and most people tend to bribe their way out because seriously who wants to get involved in matters concerning RTO and pay the whole road tax when they are actually for a short term period in another city, but all these vows can be avoided, a lesson I learnt the very hard way. It so happened that I moved to Pune and I didnt know about the correct procedure, I got caught a couple of times and then I couldnt take it anymore so I decided to get my vehicle registered in Maharashtra(as suggested by a dalal whom I met at Pune RTO). Re-registering a vehicle is not actually required but those dalal(read bastards) would say its better to get it registered to churn out more money from your pockets. The right thing to do here is just pay the road tax for another state, this sounds crazy as you are not going to stay permanently in another state but what most people don’t know is that there is a provision to get a refund if the stay is for a short time and the refund is calculated for the number of months stayed. So all you have to do is to get a NOC(no objection certificate) which is required so that the RTO knows that their are no pending cases on that vehicle or if the vehicle actually belongs to you. Getting a NOC is fairly easy, all you have to do is to go to the RTO where you vehicle is registered and they will tender the NOC in a day or two without any charges. Next when you arrive in another city then within a month pay the road tax. The papers that you need for paying the road tax are as follows
- RC book
Well and thats all their is to it. You can get everything done in a days time just keep in mind not to contact any dalals in between or you will have to pay extra and it might take more time and a lot of headaches. Also if you dont pay the road tax within a month when the NOC was tendered then the RTO will charge you extra for the months you have stayed in another city. So no re-registration is required all you have to do is to pay the road tax.
Originally posted on The ByteBaker:
This post is part of the Powerful Python series where I talk about features of the Python language that make the programmer’s job easier. The Powerful Python page contains links to more articles as well as a list of future articles.
Namespaces are a fundamental idea in Python and can be very helpful in structuring and organizing your code (especially if you have a large enough project). However, namespaces might be a somewhat difficult concept to grasp and get used to if you’re new to programming or even coming from another programming language (in my case, Java). Here’s my attempt to make namespaces just a little easier to understand.
What’s in a name?
Before starting off with namespaces, you have to understand what Python means by a name. A name in Python is roughly analogous to a variable in just about any other language, but with a few extras. First…
View original 1,010 more words
Actually, a concise rule for Python Scope resolution, from Learning Python, 3rd. Ed.. (These rules are specific to variable names, not attributes. If you reference it without a period, these rules apply)
L. Local. (Names assigned in any way within a function (
lambda), and not declared global in that function.
E. Enclosing function locals. (Name in the local scope of any and all enclosing functions (
lambda), form inner to outer.
G. Global (module). Names assigned at the top-level of a module file, or declared global in a
def within the file.
B. Built-in (Python). Names preassigned in the built-in names module :
So, in the case of
code1 class Foo: code2 def spam..... code3 for code4..: code5 x()
The for loop does not have it’s own namespace. It would look in the LEGB order,
L : local, in the current def.
E : Enclosed function, any enclosing functions(if def spam was in another def)
G : Global. Were there any declared globally in the module?
B : Any builtin x() in Python.