Name:
[002] Ian Ozsvald
Member:
91 months
Authored:
181 videos
Description:
I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_:
.. image:: http://procasts.co.uk/media/procasts_sma ...
Variable Scope [ID:923] (5/6)
in series: Python Beginners - Functions
video tutorial by Ian Ozsvald, added 11/08
Name:
[002] Ian Ozsvald
Member:
91 months
Authored:
181 videos
Description:
I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production ...
Our authors tell us that feedback from you is a big motivator. Please take a few moments to let them know what you think of their work.
The scope for variables is important - variables can only be 'seen' in their correct scope. New programmers might find this a bit confusing, these examples should make the basic scope rules of Python clear.
We use three examples to show how scope works, how masking works (which you want to avoid) and how globals can be used to change the scope rules. I also explain why global should be avoided.
Link: global
# Variable Scope using Python 2.5
# 2008/10/22
def scope1():
"Scope example using local scope."
print "in scope1"
a = 1
print a
print "finished scope1"
scope1()
#print a # will cause error
#b = 2
#def scope2_masking_existing_variable():
# "Scope example with masking."
# print "in scope2"
# b = 3 # this b masks the external b, can't be seen outside
# # NOTE this is a possible cause of error in your program
# # design, you might do this thinking that the change to
# # b is reflected once the function finishes - it won't be!
# print b
# print "finished scope2"
#
#print b
#scope2_masking_existing_variable()
#print b # has the value 2
#c = 2
#def scope3_using_global():
# "Scope example using global to access external variable."
# #global c
# #http://www.python.org/doc/2.4/ref/global.html
# # NOTE globals make it harder to figure out where
# # your variables were declared
# # This can make debugging harder therefore not recommended
# print "in scope3"
# print c # error as c is not in the local scope (yet)
# c = 4
# print c # c is 4
# print "finished scope2"
#
#print c
#scope3_using_global()
#print c # has the value 4
Got any questions?
Get answers in the ShowMeDo Learners Google Group.
Video statistics:
- Video's rank shown in the most popular listing
- Video plays: 200 (since July 30th)
- Plays in last week: 1
- Published: 56 months ago
Thank-yous, questions and comments
If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?
You may also want to see our ShowMeDo Google Group to speak to our active users and authors.
All comments excluding tick-boxed quick-comments
Excellent video on globals/scope since I am coming from C language
Good info and demo for a beginner like myself.
Hi Gasto. The video runs for 10:19 on my machine with a cleaned cache, that's the correct length.
Can you clear your cache and try again please?
Cheers,
Ian.
The video keeps finishing before its true end.
The ShowMeDo Robot says - this video is now published, thanks for adding to ShowMeDo.
Your video has been edited. This is an automatic post by ShowMeDo.
