Re: [Tutor] simple question about scope SOLVED

2019-05-18 Thread marcus lütolf
Many thanks for this quick answer.
I unfortunatly misread the tutorial (edx Course CS1301xII, Computing in Python 
II: Control Structures)
concerning scope insofar as a NameError arises if a variable is accessed which 
was not created inside
the control structure.
Marcus.

-Ursprüngliche Nachricht-
Von: Tutor [mailto:tutor-bounces+marcus.luetolf=bluewin...@python.org] Im 
Auftrag von Alan Gauld via Tutor
Gesendet: Samstag, 18. Mai 2019 12:34
An: tutor@python.org
Betreff: Re: [Tutor] simple question about scope

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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple question about scope

2019-05-18 Thread Mats Wichmann
On 5/18/19 2:20 AM, marcus lütolf wrote:
> Dear experts
> 
> 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.
> 
> However in for loops  - also control structures - like in this
> simple example:
> 
> for i in range(1,11):
> sum = 0
> sum += i
> print(sum)
> 
> the print function "returns" the last number of range as 
> value of the variable sum created inside this for loop.
> It should have raised a Name Error instead.
> Could somebody please explain this discrepancy?

in addition to Alan's comment, in your loop you are resetting sum to
zero each time through, which is why at the end of the loop sum is the
same as the finishing value of i, not the sum of the values...




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple question about scope

2019-05-18 Thread Alan Gauld via Tutor
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] simple question about scope

2019-05-18 Thread marcus lütolf
Dear experts

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.

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

for i in range(1,11):
sum = 0
sum += i
print(sum)

the print function "returns" the last number of range as 
value of the variable sum created inside this for loop.
It should have raised a Name Error instead.
Could somebody please explain this discrepancy?
Marcus.



---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor