Thursday, November 3, 2011

Scope and Variable Binding in Python

1. Python only has two notions of scope -- global and local. This means that if a variable is declared in a function, it is bound to the function. Otherwise it is bound to the global state.

You can inspect everything that is bound globally by calling globals(), and everything that is bound locally by calling locals()

2. In the following post I will investigate variable binding and scope in python via an interactive python session. That is, by example. If you want to follow along, check out the documentation for dir() and dis.

Local modifications stay local

# define a global variable a
In [1]: a = 5
# modify a locally
In [2]: def foo():
...: a = 3
...:
In [3]: foo()
# did global a change?
In [4]: a
Out[4]: 5
# nope.

Global variables can be accessed but not modified

In [10]: a = 5

In [11]: def foo():
....: print a
....:

In [12]: foo()
5

In order to modify a function in global scope, you must declare it to be global first. The relevant difference is explained by the following byte code snippets. In the case where a is a local variable, STORE_FAST is called, whereas in the case where a is a declared global, STORE_GLOBAL is called.

Compare:

In [21]: def foo():
....: a = 3
....:

In [22]: dis.dis(foo.func_code)
2 0 LOAD_CONST 1 (3)
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE

And:

In [24]: def foo():
....: global a
....: a = 3
....:
# modification is successful
In [25]: foo()
In [26]: print a
3
In [27]: dis.dis(foo.func_code)
3 0 LOAD_CONST 1 (3)
3 STORE_GLOBAL 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE

Expressions are always in global scope.

As a review, expressions are constructs like if/while/for/try/except. The point below is made with if statements, but would apply equally to any of the others.

# b is first defined inside a trivially true if statement
In [6]: if True:
...: b = 21
...:
# b is still defined globally
In [7]: b
Out[7]: 21

# it does matter that the if statement is evaluated
In [10]: if False:
....: b = 1337
....:
In [11]: b

Out[11]: 21

This contrasts with other languages like C, where variables have block scope.

#include

int main()
{
if(1)
{
int a = 6;
printf("%d\n", a);
}
printf("%d\n", a);
return 0;

}

for example, generates a compile time error: ‘a’ undeclared at the second printf statement.

Nested Function Scope

# let’s see what happens here
In [18]: def foo():
....: a = 5
....: def goo():
....: print a
....: goo()
In [19]: foo()
Out[19]: 5

Inner functions have access to the outer functions’ local variables. This is the basis of functional closures. Similar behavior occurs for lambdas.

In [23]: def foo():
....: a = 5
....: b = (lambda : a)()
....: print b
....:

The fact that functions and lambdas have access to external scope is useful for closures, but also opens up this common bug*:

# define a sequence of functions
In [40]: fs = [(lambda n: i + n) for i in xrange(10)]

# e.g.
In [41]: fs
Out[41]:
[<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>]

# now apply one of the functions in the sequence
In [43]: fs[0](3)

Out[43]: 12 # wait...wtf?

Let’s see exactly what’s happening here, just for fun:

In [49]: [dis.dis(i.func_code) for i in fs]
1 0 LOAD_GLOBAL 0 (i)
3 LOAD_FAST 0 (n)
6 BINARY_ADD
7 RETURN_VALUE
1 0 LOAD_GLOBAL 0 (i)
3 LOAD_FAST 0 (n)
6 BINARY_ADD
7 RETURN_VALUE
.
.
.

<snipped>

The lambda loads i from globals, because i cannot be found within its local scope. But during the construction of the function sequence, i changes. By the time any of the functions are called, i has been incremented to 9, and so fs[0][3] doesn’t add 0 to 3 but rather 9 to 3. This is reflected in the byte code by the LOAD_GLOBAL instruction.

To avoid problems like this one, remember that python will always look for unknown function variables in global (or higher level function) scope.

Class vs Instance vs method scope

Scope behaves in the same way for functions as for classes and objects. In fact, functions are objects. If you have some experience with python, you probably already know how to define class variables and instance variables. Class variables are declared under the class declaration, and outside of any functions. Instance variables are defined attached to self, as follows:

class Foo(object):
# class variable
a_class_var = 'yo yo'
def __init__(self):
# instance variable
self.an_instance_var = 'whattttt'

Brief Exercise for the reader: input the above into ipython, and compare dir(Foo)with dir(Foo())

Consider the following class

class Foo(object):
# class variable
x = 'yo yo'

bar = Foo()

What is bar.x?

What is Foo.x?

Solution: Both are ‘yo yo’

How about this class?

class Foo(object):
x = 'yo yo'
def __init__(self):
# print self.x
self.x = 'whattttt'
# print self.x

bar = Foo()

Now,

What is bar.x?

What is Foo.x?

In this case, bar.x= ‘whattttt’ and Foo.x = ‘yo yo’. If you uncomment the print statements though, you’ll notice that initially self.x = ‘yo yo’. Huh?

Conceptually, we can think of Foo as a template for all of it’s instances. The class variable x, declared as ‘yo yo’ gets copied to the instance bar on creation. self.x in the initializer is not referring to the class’s x, but rather to the instance’s x. We never tamper with the class’s x at all. If you do want to change Foo’s x, you would set self.__class__.blah.

One important thing to note is that creating foo from Foo does a shallow copy of Foo’s internals. So

class Foo2(object):
hi = []
def __init__(self):
self.hi.append('1')

bar2 = Foo2()
print bar2.hi
bar3 = Foo2()
print bar3.hi
print Foo2.hi

produces the output:
[
1]
[1, 1]
[1, 1]

The class Foo2’s copy of hi was modified by its instances because on instance creation, a reference to hi was copied, but not hi itself. You may have seen this error disguised as:

In [1]: def foo(blah = []):
...: blah.append(1)
...: return blah
...:
In [2]: foo()
Out[2]: [1]
In [3]: foo()
Out[3]: [1, 1]
In [4]: foo()
Out[4]: [1, 1, 1]

End

Hopefully these examples have given you some intuition about scope in python. For more detailed explanations of the examples in this post, check out the Python Language Reference. Or try out your own explorations in ipython.

*http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ inspired by this post. Even though the title of that article seems strongly worded. It appears that Guido agrees: http://www.artima.com/weblogs/viewpost.jsp?thread=98196

107 comments:

  1. I like this post because it contains a lot of useful information to read, maybe everyone will like me. I hope this post of yours will be more appreciated by it really excellent, i enjoyed it, thanks for posting it.
    io games 4 school, Jogos de Friv, 360 jogos 2019, cá koi mini

    ReplyDelete
  2. We don't yet know which 10 teams will be a part of the 2019 MLB postseason, but we do know when they'll be playing. MLB has released the full playoff schedule for this year, and it begins on Oct. 1 with the https://jerban.com/ Wild Card Game and runs through a potential Game 7 of the World Series on Oct. 30. In other words, we won't be playing November baseball this year. By way of reminder, each Wild Card Game is a one-and-done affair, the Division Series round is a best-of-five, and each League Championship Series and the World Series are best-of-seven series.

    ReplyDelete
  3. but the transfer company Removals from Jeddah to Abha packaging all furnishings to be fully maintained during the road and does not leak any dust and any dust until you reach the new house is clean and does not have any dirtشركة نقل عفش

    شركة نقل عفش من الرياض الى قطر
    شركة نقل اثاث من الرياض الى قطر
    شركة نقل عفش بالاحساء

    ReplyDelete
  4. Wonderful post! We are linking to this particularly great article on our site. Keep posting!
    www.freesitemaker.net/how-to-make-a-website/ecommerce-website-structure

    ReplyDelete
  5. This is Very very nice article. Everyone should read. Thanks for sharing. Don't miss WORLD'S BEST BikeRacingGames

    ReplyDelete
  6. The 2020 Summer Olympics, officially known as the Games of Diving tokyo 2020 live and commonly known as Tokyo 2020 or the Recovery Olympics.

    ReplyDelete
  7. NZ Rugby hopes the 'All Blacks XV' tour will become an annual event, beginning this year, with a three-week trip that begins in late October.The high-performance benefits of this team will be significant. It will help to develop the next group of Wales vs New Zealand rugby 2020 stream players, many of whom will likely become our future All Blacks, as well as give further opportunities for our coaches and other team personnel.

    ReplyDelete
  8. IRELAND skipper Peter O’Mahony says his team will have to top their unbeaten Six Nations run if they’re to level the Test series against Australia. After their 18-9 opening Test disappointment, Ireland are looking to rebound in game two tonight in Melbourne (8pm AEST) to keep the series alive for a decider the following weekend in Sydney. Ireland haven’t beaten the Wallabies for 38 years in Australia but, having made eight changes to start their strongest available team, this represents their best chance. O’Mahony, handed IRE AUS Live Rugby the captaincy reins in the absence of injured regular skipper Rory Best, said his team needed to play better than they had in more than a year to break that long-standing record.

    ReplyDelete

  9. Really Nice Post Admin, Very helpful looking for more posts, Now I have to share some information about How To Fix “LexMark Troubleshooting Guide” problem. If you are going through this problem you can simply Lexmark Printer belgie

    ReplyDelete
  10. This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. Can you guess how much these celebrities are worth? Test your knowledge with Celebrity net worth.

    ReplyDelete
  11. Thanks for sharing it is important for me. I also searched for that from here. Visit our site AVG nummer belgie

    ReplyDelete
  12. Thanks for sharing such an Amazing information, I Couldn't leave without reading your blog. I have read another good blog, I think you have read it too. click here telefoonnummer Lexmark Printer belgie

    ReplyDelete
  13. Nice Information Your first-class knowledge of this great job can become a suitable foundation for these people. I did some research on the subject and found that almost everyone will agree with your blog.
    Cyber Security Course in Bangalore

    ReplyDelete
  14. Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
    Cyber Security Training in Bangalore

    ReplyDelete
  15. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Data Science Course Hyderabad

    ReplyDelete
  16. A computer science student can learn scope and variable binding in python, but if still unable to solve python problems they should get programming help service from an expert.

    ReplyDelete
  17. Your password gives you access to each AOL service you employ . If you've forgotten your password, you'll Change AOL Password in MacBook Pro it to urge back to your AOL account. it is also an honest idea to update your password regularly and to form sure it's unique from other passwords you employ. Still you are unable to login your AOL mail account. So you can call our AOL email support toll-free number and you can also visit our official website.

    ReplyDelete
  18. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data analytics courses in bangalore

    ReplyDelete
  19. Japan Olympics is a highly suggested deal around the world, and you are not alone to be excited for the games to begin. This should explain why there are so many Tokyo Olympic live sports , as well. Some online broadcasters are also assign for cast the Tokyo Olympics 2021. So Don’t worry if your country is not listed in above. The Olympic is one of the most popular sporting and athletic events in the world. It is also the top most-viewed TV event in the world. Millions of viewers are tune in to watch the varied sports and athletic events that are part of the traditional Tokyo Olympic games. The Olympics Opening 2021 are also being expected with increasing anticipation by fans in worldwide. Sports lover from all over the world are waiting to see their country’s play in the upcoming Olympics 2021.

    ReplyDelete
  20. Here is a full guide on how to watch Floyd Mayweather vs Logan Paul live stream fight online. Get Logan Paul vs Mayweather fight time, PPV price & more. Floyd Mayweather vs Logan Paul Live Stream

    ReplyDelete
  21. Thank you so much for sharing all this wonderful information !!!! It is so appreciated!! You have good humor in your blogs. So much helpful and easy to read!
    Python Training in Pune

    ReplyDelete
  22. how people save there whatsapp to cyber attack any warning application in 2021 ?

    ReplyDelete
  23. I see some amazingly important and kept up to a length of your strength searching for in your on the site
    best data science institute in hyderabad

    ReplyDelete
  24. Thank you quite much for discussing this type of helpful informative article. Will certainly stored and reevaluate your Website.
    Data Analytics Course in Bangalore

    ReplyDelete
  25. Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
    Data Analytics training in Bangalore

    ReplyDelete
  26. Awesome blog for knowledge. Thank you for sharing this useful article. This blog is a very helpful to me. Keep sharing this type informative articles with us.
    Data Science Training in Hyderabad
    Data Science Course in Hyderabad

    ReplyDelete
  27. In a time where boxing has spotlighted overage and long-retired fighters, social media influencers and mixed martial artists, this is a marquee event featuring the No. 1 and No. 3 welterweights in the world, one of the best of right now vs. one of the best ever( Pacquiao vs Spence jr Live ), a younger man seeking the greatest of glory taking on an older man desiring that one last dose of it.

    ReplyDelete
  28. I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur

    ReplyDelete
  29. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. Keep up the good work - COC MOD and GBWhatsapp

    ReplyDelete
  30. Thanks for sharing, well explained and written. keep Sharing
    Python Training in Pune

    ReplyDelete
  31. The NFL Fixtures 2020 and see where your American football 32 team stands in the race for the with all the latest news, stream, results, schedules, fixtures, and tables.

    Visit my site>> https://www.nflfixtures.com/

    ReplyDelete
  32. I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end.
    Python Classes in Pune

    ReplyDelete
  33. Wow, fantastic weblog structure! How long have you evver been running a blog for?
    you made blogging look easy. The total glance of our web site is magnificent, as well as the content!
    경마사이트
    경마

    ReplyDelete
  34. Anthony Joshua will defend his WBA, WBO and IBF heavyweight titles against the formidable threat of former undisputed cruiserweight champion Oleksandr Usyk on Sept. 25 at Tottenham Hotspur Stadium, London — and DAZN will be showing the Joshua vs Usyk Live Stream in over 170 countries on fight night.

    ReplyDelete
  35. Thanks for posting the best information and the blog is very good.data science course in Lucknow

    ReplyDelete
  36. The global Industrial Wearable Devices Market size is expected to reach $8.40 billion by 2027 from $3.79 billion in 2019, growing at a CAGR of 12.4% from 2020 to 2027. Industrial wearable devices are designed to improve workplace productivity, safety, and efficiency in sectors such as manufacturing, logistics, mining, and aerospace & defense. Industrial wearables are products that can be fitted on the human body with ease and through which real time information can be obtained or tracked. These wearables comprise various sensors, controllers, and monitoring platforms, which track data and store it on cloud. Integration of wearable technology with management systems such as CRM and facial recognition is on the rise for effective communication.

    ReplyDelete
  37. Produce a construction to shows your online free reference generator help logically. This may aid you a full heap of faces around the issue and conjointly typically doesn't set off this space. Even as the niche can presently be time tested instead of being useful for you in the person. Once developing a well-suited draft on the mission, update this, and make certain it's cheap and contains everything at your pay to do my homework.

    ReplyDelete
  38. Great post happy to see this. I thought this was a pretty interesting read when it comes to this topic Information. Thanks..
    Artificial Intelligence Course

    ReplyDelete
  39. Nice Post thank you very much for sharing such a useful information and will definitely saved and revisit your site and i have bookmarked to check out new things frm your post.
    Data Science Course

    ReplyDelete
  40. Very great post which I really enjoy reading this and it is not everyday that I have the possibility to see something like this. Thank You.
    Best Online Data Science Courses

    ReplyDelete
  41. Very informative blog! There is so much information here that can help thank you for sharing.
    Data Science Syllabus

    ReplyDelete
  42. I believe you are an expert in this field before you are able to put up this well detailed information on this topic. Best Universities In The West Coast USA

    ReplyDelete
  43. Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles. Anyway, I will subscribe to your feed and hope you post again soon.
    Data Scientist Course in India

    ReplyDelete
  44. This is the right website for anybody who really wants to find out about this topic. You know a whole lot about this topic. Wonderful post, it’s just excellent. Laundry Memes Funny

    ReplyDelete
  45. Thanks for sharing your amazing thoughts. It’s a really nice and well-explained blog. Are you looking for garbage pickup near tehran, Tehran province 

    ReplyDelete
  46. When your website or blog goes live for the first time, it is exciting. 경마사이트

    ReplyDelete
  47. It is extremely nice to see the greatest details presented in an easy and understanding manner 토토사이트

    ReplyDelete
  48. We stumbled over here from a different web address and thought I should check things out. 바둑이게임

    ReplyDelete
  49. This is truly an practical and pleasant information for all and happy to see this awesome post by the way thanks for sharing this post.
    Data Scientist Course in Noida

    ReplyDelete
  50. Become a Data Science expert with us. study Data Science Course in Hyderabad with Innomatics where you get a great experience and better knowledge.

    ReplyDelete
  51. This post provides clear idea in favor of the new people of blogging, that really how to do blogging and site-building. 카지노사이트

    ReplyDelete
  52. the GBWhatsApp Pro, another version of WhatsApp that gives the users of the original WhatsApp to enjoy more features.
    GBWhatsApp Pro APK

    ReplyDelete
  53. Impressive. Glad to see such content After a long time and have a look on Navy Quick Links to get more information.

    ReplyDelete
  54. Excellent site you’ve got here. It’s difficult to find talented writers like you nowadays. I sincerely appreciate people like you! Keep it up. Checkout Wish a friend Good Evening Message

    ReplyDelete
  55. I love your blog.. very nice colors & theme. Did you create this website yourself
    or did you hire someone to do it for you? Plz reply as I'm
    looking to create my own blog and would like to find out where u got this from.
    thanks a lot


    카지노사이트
    안전카지노사이트
    카지노

    ReplyDelete
  56. I really like looking through a post that can make people think.
    Also, thank you for allowing for me to comment!


    스포츠토토
    토토사이트
    먹튀검증

    ReplyDelete
  57. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work thank you.
    Data Analytics Course in Chandigarh

    ReplyDelete
  58. Thank you for sharing this much of knowledge ;
    please visit:- Java Classes In Pune

    ReplyDelete
  59. Thanks for Content its useful to every Reader,,
    For more Click Here

    ReplyDelete
  60. I curious more interest in some of them hope you will give more information on this topics in your next articles.
    full stack development course

    ReplyDelete
  61. This is definitely one of my favorite blogs. Every post published did impress me.
    Data Science Course in Indore

    ReplyDelete
  62. It is perfect chance to make a couple of game plans for the future and the opportunity has arrived to be sprightly. I've scrutinized this post and if I may I have the option to need to suggest you some interesting things or recommendations. Perhaps you could create next articles insinuating this article. I have to examine more things about it!

    ReplyDelete
  63. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

    ReplyDelete
  64. Very informative Blog! There is so much information here that can help thank you for sharing.
    Data Science Training in Lucknow

    ReplyDelete
  65. Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles. Anyway, I will subscribe to your feed and hope you post again soon.
    Data Scientist Course in India

    ReplyDelete
  66. First I appreciate your work very much. I read your post carefully and I must say that you are doing a great job by sharing your thoughts with us. Please check out does sprouts take ebt

    ReplyDelete
  67. This site was… how do I say it? Relevant!! Finally, I have found something that helped me. list of veterinary schools

    ReplyDelete
  68. Troubleshooting was the first testing fashion at that time and remained so for the coming twenty times. By the Eighties, development brigades sounded former detaching and fixing programming bugs to testing purposes in true settings. It arrange for a redundant in depth perspective on testing, which included a top quality protestation course of that was vital for the product development life cycle.
    For More Visit : Software Testing Classes in Pune

    ReplyDelete
  69. I see the article has a great deal of investment in content and science. I took the time to read them and found them quite interesting. You can check out walmart key copy hours

    ReplyDelete
  70. I think this is a really good article. You make this information interesting and engaging. Thanks for sharing.
    Data Science Course in India

    ReplyDelete
  71. This is a very nice one and gives in-depth information. I am really happy with the quality and presentation of the article. I’d really like to appreciate the efforts you get with writing this post. Thanks for sharing.
    Salesforce Training in Pune

    ReplyDelete
  72. Thanks for sharing this amazing content. Your information is really very outstanding to read. Keep it up and best of luck with your future updates. nyif dashboard

    ReplyDelete
  73. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work thank you.
    Business Analytics Course in Chandigarh

    ReplyDelete
  74. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it, Keep posting new articles.
    Data Scientist Course in Jalandhar

    ReplyDelete
  75. Yes! it's truly noteworthy substance. A very a debt of gratitude is in order for sharing this sort of post and investing a particularly significant energy in investigating a particularly novel substance, keep update like this I am curiously hanging tight for your next post. Keep it up!
    economics assignment help

    ReplyDelete
  76. Your articles are really amazing to read have full of information.

    website
    website
    website

    ReplyDelete

  77. Maintain the high standard you've set with your writing! Please take a look at my blog as well if you have the time.
    https://crackguru.net/office-tab-enterprise-13-10-crack/

    ReplyDelete
  78. I think it’s right here, and it’s only going to proceed to get greater. There’s no stopping that freight prepare now,” Brad Alberts, the CEO and president of the Dallas Stars, told ESPN final year. Though technically authorized by federal requirements, alcohol commercials exhibiting folks truly consuming are subject 카지노 to restrictions by tv networks, Armbruster said. And American federal regulation continues to ban tv commercials for cigarettes. Technology seems to be working its means into each side of our lives.

    ReplyDelete
  79. Our educational website Do My Homework has been a great platform for students of all subjects and degrees. We provide exceptional support to students studying in various universities in the United States, the United Kingdom, and Australia. As per the latest research done by Google, we have been ranked as the best online organization which is helping millions of students across the globe.

    We hire some of the best professionals who are experts in various subjects and can create the most suitable assignments for our clients. They follow the word-by-word instructions given by the clients and develop assignments accordingly so that students get the exact work they were looking for. There is no scope of getting plagiarism as our experts make individual copies for each of our clients. They pay dedicated attention to all work and create an original masterpiece for each client.

    ReplyDelete
  80. Thank you for these wonderful updates; keep it up, it’s a clear and understandable informative review. Thanks for sharing your ideas and which viewers will actually make use of it. Nice post, fce eha-amufu cut off mark for computer engineering

    ReplyDelete
  81. Thank you for sharing this much of knowledge ;
    please visit:- Java Classes In Pune

    ReplyDelete
  82. Thank you for sharing your expertise! Keep up the excellent work! Continue to share. Please feel free to look at my website.
    Abogado Familia Cerca Mi Iselin NJ

    ReplyDelete
  83. The Turkey visa cost for Indian citizens varies according to the kind and the term of the visa requested. It is strongly advised to visit our website for the most up-to-date and correct information on visa costs and application procedures.

    ReplyDelete