4 Replies - 273 Views - Last Post: 13 July 2015 - 04:40 AM Rate Topic: -----

#1 Jabutosama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 15

Imported files affecting global values

Posted 11 July 2015 - 04:12 AM

I have main.py. It is able to import other python files into it. It is ready so far.


But how is this done?
Let's say, we 1st.py, 2nd.py and 3rd .py.

1st.py makes new variable a="".
2nd.py changes a="hello world".
3rd.py prints a.

error occur naturally in 2nd.py and 3rd.py if a is not defined. (1st.py defines it first time)

This may look strange but this logic is needed for my game project.

This may look strange but this logic is needed for my game project.

Is This A Good Question/Topic? 0
  • +

Replies To: Imported files affecting global values

#2 andrewsw  Icon User is online

  • I'm not here to twist your niblets
  • member icon

Reputation: 4708
  • View blog
  • Posts: 17,477
  • Joined: 12-December 12

Re: Imported files affecting global values

Posted 11 July 2015 - 04:45 AM

This is a typical concern when reliant upon global variables.

Firstly, why would the variable not be defined?

You could write code in the other files that first checks for the presence of the variable. Can you recover if it is missing though? That is, can you ask for, or otherwise derive, the value, and allow the code to continue?

A missing global variable is typically too significant to allow the application to continue. It would be checked for at an early stage and, if not found, the code, the application, would be exited, or even allowed to error-out.

Again, typically, a global variable would be checked for and assigned at an early stage. If it cannot be assigned then the application would gracefully exit. The rest of the code would assume that the variable existed. The fact that the application may error out would be considered acceptable, because it means there is a significant issue with the code. (Some may disagree with the previous sentence, preferring to constantly check and exit gracefully.)
Was This Post Helpful? 0
  • +
  • -

#3 Jabutosama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 11-July 15

Re: Imported files affecting global values

Posted 11 July 2015 - 04:50 AM

Main.py exists only for importing other files. The other files are the actual 'code' to 'do the desired thing'.
Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 153
  • Joined: 27-December 13

Re: Imported files affecting global values

Posted 13 July 2015 - 03:35 AM

You can pass the variable in and out of the python modules, something like:
a = first()  # a is created in module 'first'
a = second(a)  # a is modified inj module 'second'
third(a)  # module 'third' do whatever - for instance: print(a)


Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 6300
  • View blog
  • Posts: 13,465
  • Joined: 16-October 07

Re: Imported files affecting global values

Posted 13 July 2015 - 04:40 AM

Hmm... I suppose the most basic option would be to abuse a module.

e.g.
# -------------------------
# State.py 
a = 'Init a'
b = 'Init b'

def dump():
    print("State a={0}, b={1}".format(a, b ))

# -------------------------
# fileModMadness1.py
import State

print(__name__)
State.dump()

State.b = 'feel the burn'

State.dump()
EOT

# -------------------------
# fileModMadness2.py
import State
import fileModMadness1

print(__name__)
State.dump()

State.b = 'more pain'
State.x = 'add crap'

State.dump()
print(State.x)



Results:
fileModMadness1
State a=Init a, b=Init b
State a=Init a, b=feel the burn
__main__
State a=Init a, b=feel the burn
State a=Init a, b=more pain
add crap




Alternately, you can use a basic object. This will offer a number of advantages.
e.g.
# -------------------------
# State.py
class StateImpl(object):
    def __init__(self):
        self.a = 'Init a'
        self.b = 'Init b'

    def dump(self):
        print("State a={0}, b={1}".format(self.a, self.b ))

State = StateImpl() # our awful global state

# -------------------------
# fileModMadness1.py
from State import *

print(__name__)
State.dump()

State.b = 'feel the burn'

State.dump()
EOT

# -------------------------
# fileModMadness2.py
from State import *
import fileModMadness1

print(__name__)
State.dump()

State.b = 'more pain'
State.x = 'add crap'

State.dump()
print(State.x)



Results:
fileModMadness1
State a=Init a, b=Init b
State a=Init a, b=feel the burn
__main__
State a=Init a, b=feel the burn
State a=Init a, b=more pain
add crap



Now we can address a weakness in our dump programmatically. We can also add some interesting debug code.

e.g.
# -------------------------
# State.py
import inspect

class StateImpl(object):
    def __init__(self):
        self.a = 'Init a'
        self.b = 'Init b'

    def __setattr__(self, name, value):
        print("set from {2}, State.{0}={1}".format(name, value, inspect.stack()[1][1]))
        self.__dict__[name] = value

    # def __getattr__(self, name):

    def dump(self):
        print("State " + (','.join("{0}={1}".format(k, self.__dict__[k]) for k in self.__dict__.keys())))

State = StateImpl() # our awful global state



Results:
set from /home/baavgai/Dev/python/State.py, State.a=Init a
set from /home/baavgai/Dev/python/State.py, State.b=Init b
fileModMadness1
State a=Init a,b=Init b
set from /home/baavgai/Dev/python/fileModMadness1.py, State.b=feel the burn
State a=Init a,b=feel the burn
__main__
State a=Init a,b=feel the burn
set from fileModMadness2.py, State.b=more pain
set from fileModMadness2.py, State.x=add crap
State a=Init a,x=add crap,b=more pain
add crap



With a global object you limit your self inflicted pain and suffering to a single point. Most importantly, you can actually make changes to that point that allow for debugging, which is where globals torture you the most.

Also, relating to the original question, you can override getattr and define default behavior for undefined values.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1