• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
King Mir

Python global variable problem

11 posts in this topic

So I have 3 files like so:
[source lang="python"]#Test1.py
import Test2, Test.Test3

if __name__ == '__main__':
Test2.initVal()
print Test2.getVal()
Test.Test3.func()[/source]
[source lang="python"]#Test2.py
val = {}

def initVal():
val[0] = 1

def getVal():
return val[0][/source]
[source lang="python"]#Test/Test3.py
import Blobs.Test2

def func():
print Blobs.Test2.getVal() #blobs is top level package[/source]

This prints 1, then gives me an exception, because func() sees val as empty the second time. How do I make it see the properly changed variable?

EDIT: clarified behavior Edited by King Mir
0

Share this post


Link to post
Share on other sites
This is because you need to specifiy you are using a global.

Your initVal and getVal functions should look like this:

[CODE]
def initVal():
global val
val[0] = 1

def getVal():
global val
return val[0]
[/CODE]
1

Share this post


Link to post
Share on other sites
[quote name='renega_666' timestamp='1340735186' post='4953088']
This is because you need to specifiy you are using a global.

Your initVal and getVal functions should look like this:

[CODE]
def initVal():
global val
val[0] = 1

def getVal():
global val
return val[0]
[/CODE]
[/quote]Tried it, didn't work.

From what I understand, global is used to spefiy that assign ment should overide a global variable. It would be needed to make Test2.getVal() work the first time, if the dictionary was reassigned, instead of having a key added. Here Test2.getVal() works, just not Blobs.Test2.getVal(),
0

Share this post


Link to post
Share on other sites
This is working fine for me, both function return 1. What is the exact error. What do you mean by it does not work? Could you provide the project structure? (packages layout)

[quote]
[left][background=rgb(250, 251, 252)]From what I understand, global is used to spefiy that assign ment should overide a global variable. It would be needed to make Test2.getVal() work the first time, if the dictionary was reassigned, instead of having a key added[/background][/left]
[/quote]

I'm not sure to understand what you mean. Global is used to tell the interpreter that you are not dealing with a new object but reusing/overriding one that was defined at global scope when the module was imported the first time. So in any case (assignement or adding a key), "global val" is needed.
1

Share this post


Link to post
Share on other sites
The package layout is that Test3.py is in a package Test, and everything is in a package Blob (including package Test).

The error is that it prints 1, then throws a key error for the second call.
0

Share this post


Link to post
Share on other sites
I also found this surprising behaviour:

[source lang="python"]#Test5.py
import Blobs.Test2

def func2(atype):
print atype
print Blobs.Test2.TestClass
print atype == Blobs.Test2.TestClass[/source]
[source lang="python"]#Test2.py
class TestClass (object):
pass[/source]

[source lang="python"]import Test5, Test2

if __name__ == '__main__':
Test5.func2(Test2.TestClass)[/source]

This prints 2 different class paths, followed by False. Why? It's the same class, just a different way of getting to it.
0

Share this post


Link to post
Share on other sites
It seems all those issues come from the fact that you are importing the Test2.py differently.

Sometimes you use [CODE] import Blobs.Test2 [/CODE] and sometimes [CODE] import Test2 [/CODE]

Python treats it as if it were two different modules. To fix your problem always use import Blobs.xxx (where xxx is the name of your module/package)
1

Share this post


Link to post
Share on other sites
I see. Thanks.

That's very annoying. It means I'll have to use absolute paths for all imports. And probably use from import for brevity,
0

Share this post


Link to post
Share on other sites
If you plan to distribute the package (or use it in different projects), yes. Otherwise you can just omit the package name (in this case you should even remove the __ini__.py as it is not a real package anymore). Edited by renega_666
0

Share this post


Link to post
Share on other sites
Do you mean leave the folder structure, but remove the __init__.py from the Test folder? Or remove the Blobs package but leave Test intact?

Right now I have a package, like Test in this question, that needs to call a function 1 package up. Eventually I might add enough to the program that that function and related code would warrant a package of its own. So the solution needs to have that in mind.

Though I am using packages for organisational convenience, not really to be able to reuse the code in another program.
0

Share this post


Link to post
Share on other sites
I mean removing the __init__.py from the blobs folder and replacing

[CODE]
#Test/Test3.py
import Blobs.Test2

def func():
print Blobs.Test2.getVal() #blobs is top level package
[/CODE]

by

[CODE]
#Test/Test3.py
import Test2

def func():
print Test2.getVal() #blobs is not a top level package anymore, this is just the project dir
[/CODE]

When writing a python application, I usually follow this layout:

[CODE]
ProjectDir/
main.py
config.py
package1/
__init__.py
file1.py
file2.py
package2/
__init__.py
file1.py
file2.py
[/CODE]

As package1 and package2 directories contains a __init__.py, I can do the following in the main script:

[CODE]
# main.py
import package1.file1
import package1.file2
import package2.file1
import package2.file2
def test():
package1.file1.a_func()
package1.file2.a_func()
package2.file1.a_func()
package2.file2.a_func()
[/CODE]

And nothing prevents me to use the configuration module from the package1.file1 module:

[CODE]
import config
"""
no need to specify path as config.py sits in the same directory as the main.py (path is added to
sys.path automatically when the main script is started so python knows about all other files on the same level as the main script)
"""
def a_func():
print(config.a_value)
[/CODE]

Edit: clarified last docstring + a typo Edited by renega_666
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0