[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] setting + modifying a variable for use across the entire application



ah you're right, the actual code is

if android:
    bla
    bla

I was writing that from memory a bit.

I'll have to dig in more to see the differences between our code.
here's what was happening though in general -- the  problem

1. main module imports settings as blank
2. main module imports child modules
3. when imported, child modules import settings, also as blank
4. main module modifies settings

so the child modules were importing the blank version of the module
rather than the verision modified later. I changed it to:

1. main module imports settings
2. main module modifies settings
3. main module imports child modules

this is working.





On Fri, Dec 16, 2011 at 5:55 PM, Christopher Night
<cosmologicon@xxxxxxxxx> wrote:
> Huh, that's really weird. I use this technique all the time, and I never
> worry about import order or having app-level values pre-set, and it always
> works perfectly as expected for me.
>
> $ cat settings.py
> sound_volume = 1000
>
> $ cat module.py
> import settings
> def printvolume():
>     print settings.sound_volume
>
> $ cat main.py
> import module, settings
> module.printvolume()
> settings.sound_volume = 3
> module.printvolume()
>
> $ python main.py
> 1000
> 3
>
> Not sure what you're doing different from me, but it might be worth looking
> into. There could be some other issue. Can you post a working example (like
> I did) where it behaves different than you expect?
>
> I also don't understand why you're testing android == True in your code.
> That should never be the case. If the import is successful, android refers
> to some module, not the value True.
>
> -Christopher
>
>
> On Fri, Dec 16, 2011 at 3:34 PM, Sean Wolfe <ether.joe@xxxxxxxxx> wrote:
>>
>> BTW guys, this is working for me. I'm using a module as an
>> application-level variable store. In my top-level main.py, I'm setting
>> variables that are referenced by modules down the line.
>>
>> I had to do a couple things to make it work. One, the app-level
>> variables can't be already defined when imported, they have to be new
>> values which are set after the import by main.py. Two, any subsequent
>> modules which are imported have to import AFTER I'm done creating my
>> "app variables".
>>
>> So it looks like this:
>>
>> -----
>> main.py:
>>
>> import acolyte_settings
>> try:
>>    import android
>> except ImportError:
>>    android = False
>> if android == True:
>>    acolyte_settings.ANDROID = True
>>    acolyte_settings.ASSETS_DIR = './assets'
>>
>> import gameloop
>>
>> def (main):
>>    gameloop.gameloop()
>>
>> Now gameloop can use the acolyte_settings module to find out the
>> application settings. I don't have to write to an external file, or
>> constantly pass 'android=True' to all my classes. It's not all that
>> practical for multiple changes to the app-level variables, but for my
>> purposers, I like it!
>>
>> Thanks for your help on this guys.
>
>



-- 
A musician must make music, an artist must paint, a poet must write,
if he is to be ultimately at peace with himself.
- Abraham Maslow