[Tutor] simple question about scope

Alan Gauld alan.gauld at yahoo.co.uk
Sat May 18 06:33:49 EDT 2019


On 18/05/2019 09:20, marcus lütolf wrote:

> in learning the principles of Python I came across scope in the
> control structure's section.
> There I read the notion that variables createted inside a 
> control structute can't be seen or accessed from outside that
> structure, Python would raise a Name Error.

I don't know which tutorial you are reading but that's just wrong.
Names created inside functions are local to that function.
Names created inside a module are local to that module
(and rather confusingly referred to as global scope)
Names created inside a class are local to that class.
Names created inside a generator expression are local to that expression.

That's pretty much all you need to know about python scoping.

> However in for loops  - also control structures - like in this
> simple example:

Control structures like for/while/if-else etc have
no influence no name visibility.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list