Modules problem

This is the place for queries that don't fit in any of the other categories.

Modules problem

Postby HairyDrummerGuy » Wed May 01, 2013 8:42 am

(I failed, replace the word module with function!)
Okay, I'm trying to get my head round modules here. Kinda embarrassing.
So here is a module from a program I am writing:
Code: Select all
def load_game():
    file = open(lvl)
    game_progress = file.read()
    file.close()
    print(" ")
    print("Game loaded at level " + str(game_progress) + "!")
    print(" ")
    game_play()


The part I am struggling at is variables. I want to use 'game_progress' at other points in the program, however it doesn't carry over the whole program, as it's local(?).
I tried making it global by adding 'global game_progress' at the start of modules that used it, however, this had it's problems, and I have heard global variables are bad practice.

I know the brackets at 'def load_game():' can help with this but I don't quite know how. Do I put game_progress in there? No idea!

Sorry for my vague knowledge of python, I'm a beginner, and I've been doing well, but now I've started using modules I need to learn more about them.

Thanks for the help!
Last edited by HairyDrummerGuy on Wed May 01, 2013 9:22 am, edited 1 time in total.
HairyDrummerGuy
 
Posts: 6
Joined: Tue Mar 19, 2013 2:30 pm

Re: Modules problem

Postby stranac » Wed May 01, 2013 9:20 am

That isn't a module, it's a function.
A module is a single .py file that you import.

There are a few ways you can handle this, but I believe the best one is using a class, and storing the game progress as an instance attribute, like so:
Code: Select all
class Game(object):
    ....
    def load_game(self):
        ...
        self.progress = whatever

    def method_using_progress(self):
        # do stuff with self.progress


Another possibility would be returning the progress from your function, and passing it to other functions that need it, something like this:
Code: Select all
def load_game():
    # do stuff
    return game_progress

progress = load_game()
do_other_stuff(progress)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Modules problem

Postby micseydel » Wed May 01, 2013 9:57 am

stranac wrote:A module is a single .py file that you import.

Not always. It could be a directory with a __init__.py file.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Modules problem

Postby stranac » Wed May 01, 2013 10:17 am

micseydel wrote:
stranac wrote:A module is a single .py file that you import.

Not always. It could be a directory with a __init__.py file.

No, that's a package.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Modules problem

Postby micseydel » Wed May 01, 2013 10:22 am

A quick look at the standard docs indicates you're right. My mistake.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Modules problem

Postby HairyDrummerGuy » Thu May 02, 2013 9:24 am

stranac wrote:That isn't a module, it's a function.
A module is a single .py file that you import.

There are a few ways you can handle this, but I believe the best one is using a class, and storing the game progress as an instance attribute, like so:
Code: Select all
class Game(object):
    ....
    def load_game(self):
        ...
        self.progress = whatever

    def method_using_progress(self):
        # do stuff with self.progress



Can you explain the class method more? I'm rather confused. What does each line do and what do I put to set the game_progress, and how do I change it and use it after that? Thanks for the help. If it helps to explain it better, ignore the code I posted and just explain from scratch. Eventually I will click :P
HairyDrummerGuy
 
Posts: 6
Joined: Tue Mar 19, 2013 2:30 pm

Re: Modules problem

Postby Kebap » Thu May 02, 2013 1:46 pm

My advise is to postpone classes and go try the "return" alternative first. This will do the trick. Worry about classes later.

Code: Select all
def greet(name):
  return "hello " + name

x = greet("world")
print x

In a way, the brackets can be used for input into the function, while "return" will create output. Maybe check a tutorial. :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 403
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Modules problem

Postby HairyDrummerGuy » Thu May 02, 2013 4:57 pm

Kebap wrote:My advise is to postpone classes and go try the "return" alternative first. This will do the trick. Worry about classes later.

Code: Select all
def greet(name):
  return "hello " + name

x = greet("world")
print x

In a way, the brackets can be used for input into the function, while "return" will create output. Maybe check a tutorial. :mrgreen:


Thanks for the help :D Messing about with this in the shell so I can really work out how it works.

Finally got a solution!
HairyDrummerGuy
 
Posts: 6
Joined: Tue Mar 19, 2013 2:30 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 5 guests