🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.python
2 messages
2 total messages Started by ltklos Thu, 04 Sep 2014 05:49
zmienna globalna
#28519
Author: ltklos
Date: Thu, 04 Sep 2014 05:49
64 lines
2053 bytes
Witam, zaczynam dopiero z Pythonem i mam problem ze zmienną globalną.

mam dwa pliki z kodem: 
moduly1.py ktory zawiera jedna definicje funkcji:

def f (a,b):
    import test_global
    if a == b:
        test_global.x = 0
    elif a < b:
        test_global.x = -1
    else:
        test_global.x = 1

    print ".x = %d" % (test_global.x)

    return a + b

i global_test.py:

from moduly1 import f
x = 10
print "1. x = %d" % (x)
print "suma: %d" % (f (1,2))
print "2. x = %d" % (x)
print "suma: %d" % (f (2,2))
print "3. x = %d" % (x)
print "suma: %d" % (f (3,2))
print "4. x = %d" % (x)

może ktoś mi wyjaśnić dlaczego w wyniku odpalenia global_test.py zamiast otrzymać:
1. x = 10
.x = -1
suma: 3
2. x = -1
.x = 0
suma: 4
3. x = 0
.x = 1
suma: 5
4. x = 1

dostaję taki wynik:

1. x = 10
1. x = 10
.x = -1
suma: 3
2. x = -1
.x = 0
suma: 4
3. x = 0
.x = 1
suma: 5
4. x = 1
.x = -1
suma: 3
2. x = 10
.x = 0
suma: 4
3. x = 10
.x = 1
suma: 5
4. x = 10
Re: zmienna globalna
#28520
Author: Jordan Szubert
Date: Thu, 04 Sep 2014 06:33
10 lines
1205 bytes
Dnia 04-09-2014 o 07:49:21 <ltklos@gmail.com> napisał(a):

> Witam, zaczynam dopiero z Pythonem i mam problem ze zmienną globalną.
[>8]

coś z tych okolic?
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#executing-the-main-module-twice

-- 
Jordan Szubert
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads