Python global variables. Error in python?

I recently created this example and am surprised at the results. I will demonstrate this with code:

File1: b.py :

delta = 0.0

def example():
    global delta

    delta = 1

def ret_delta():
    return delta

File2: a.py :

from b import *

example()

#WHY ARE THESE DIFFERENT?
print(delta) # prints: 0.0
print(ret_delta()) # prints: 1

This makes no sense! Why does accessing a variable and calling a function returning this variable have any meaning?

For your reference, I am using python 3.5.2 for Windows 32 bit

+4
source share
2 answers

When you do

from b import *

a.py, , b ( delta) a. float Python, a.delta b.delta, .   , print() a.delta, b.delta.

+5

from b import * a. b name. .

, Python - ; "" . , . , b a, from b import * .

< delta b. 0.0 float. , delta a.

, a.delta b.delta; b.delta = 1 a.delta, .

, , Python; . Python Ned Batchelder.

+2

Source: https://habr.com/ru/post/1650301/


All Articles