Let’s say I want to try something on a list in Python. While I usually like to call my test list objects “mylist”, I sometimes forget, and create a variable named “list”:
If you’re like me, then you might not immediately notice that you’ve just defined a variable whose name is the same as a built-in type. Other languages might have defined “list” as a reserved word, such that you cannot define it. (Just try creating a variable named “if”, and you’ll see what I mean.) But Python won’t stop you. This means that now, instead of type(list) returning “type” (i.e., indicating that list is a data type), it’ll say:
What’s going on here? How was I able to turn lists into strings? And how did deleting “list” suddenly restore things? The answer is: Python’s scoping rules. They are fairly simple to understand, and very consistent (as you would expect from Python), but have implications that can cause all sorts of weirdness if you’re not sure of what to expect. The Python scoping rules are Local, Enclosing Function, Global, and Builtins, often abbreviated as “LEGB.” This means that when Python encounters an identifier (variable or function) name, it will look for the name as follows:
If you have defined a function within a function (and beginners really shouldn’t be doing this), then Python will look in the enclosing function,
As a last resort, Python looks in the __builtins__ module, the namespace in which Python’s standard and built-in types are located.
there are no functions, and thus no L or E to consider. Python will look in G, the global namespace, meaning the current file. If there is an x defined there, then great; that’s what will be printed. If there is no x defined in the current file, then Python will look in __builtins__, which doesn’t have an x, and we’ll get a NameError exception — meaning, Python doesn’t know what name you’re talking about. So far, so good, right? Well, now consider what happens when we define a new variable, by assigning it a value. If you define the variable inside of a function, then it’s in the “local” scope. If you define the variable at the top level of a file, then it’s in the “global” scope. And if you define it in the __builtins__ module (and you really shouldn’t be doing that), then it’ll be in the “builtins” scope. When we defined “list” (e.g., “list = ‘abc'”), we were defining a new variable in the global scope. We didn’t replace or remove the builtin “list” at all! Indeed, the builtin “list” is still available if we use its full name, __builtins__.list:
The problem, then, isn’t that we have replaced the built-in “list”, but rather that we have masked it. Once we have defined “list” in the global scope, all naked references to “list” in that file — as well as in functions defined within that file — will see our global variable “list”, rather than __builtins__.list. How, then, did it help for me to delete “list”? Because del(list) doesn’t delete __builtins__.list. (You can do that, by the way, but that’s for another blog post.) Rather, del(list) in our case deletes “list” from the global scope. When we then ask Python for type(list), it looks in L, E, and G, and doesn’t find anything. It thus goes to __builtins__, finds “list”, and returns us the type of “list”, which is once again a “type” or “class”. Whew! If you enjoyed this, then you might like my three-day, online Python class, which includes such tidbits. The course will begin on January 14th (read more about it at masterpython.com). You can also subscribe to my free, exclusive technology newsletter.
Direction; Put oven on high broil, let heat for 5-10 min while you prepare the salmon. Wash salmon with cold water, then blot dry and set a side. Mix dill, organic seasoning, crushed red pepper together and set a side. Cut lemon in thin slices, Cut the butter in thin slices. Put the season mixture on both sides of the salmon steaks and then arrange them in the baking pan. Place the lemon slices on the salmon steak, Place butter slices on the salmon steaks. Put the pan with the salmon steaks in the oven on the top rack, Broil for seven minutes (7 minutes). Remove and serve. (I had it with cherry tomatoes, orange slices and tea).
Thank you to everyone that entered – I was blown away with the response! And a HUGE thank you to Garrett, Your Blog Mechanic, Bespoke Packaging & Laugh & Grow Press for your generous contributions. Have a great weekend. Kelly
I’m so excited to announce the launch of my new action set “Little Pieces – Workflow”! It will be on sale from Monday 13th January 2014.
To celebrate I am giving away an entire set along with these other amazing prizes from Garrett, Your Blog Mechanic, Bespoke Packaging and Laugh & Grow Press.
I have chosen these three businesses to be apart of this giveaway as they were all involved in the revamp that my business so desperately needed in 2013.
Garrett is A wiz when it comes WordPress – something I’m absolutely terrible with and just didn’t have the time to learn. He gave my blog a complete overhaul and fine tuned my SEO so that I’m now on the first page of Google for potential clients to find me. His prize will give you the tools to get you the best possible Google ranking for your business in 2014. Bespoke Packaging offer simple yet stylish and elegant packaging options that can be customised for your business which is why I love them, they have given my products the finishing touch that my clients love and appreciate. And Laugh & Grow Press is responsible for my new logo which I absolutely adore and all of the other beautiful features on my blog. She has kindly donated her Simply Announcing Template Collection.
The competition will close on Friday 10th January at 10pm (Australian Eastern Standard Time). The winner will be selected using Random.org and announced on my facebook page.
This entry was posted in Babies, Newborns, Products and tagged Actions, Actions for Newborns, baby, baby photography, Brisbane baby photographer, Brisbane newborn photographer, Brisbane newborn photography, Brisbane photographer, little pieces photography, Maternity photographer, multiples, newborn, newborn photography, newborn photography workshop australia, newborn posing class, Photoshop Actions, products.