mask_man
mask_man

Reputation: 51

Python - How to alter variable in a module imported as "from module import *"

"""Making a better version of my IF(Interactive Fiction) module so you can just create a module that imports it and you can start adding crap Three things- Well four if you count the DEBUG variable- The variables PLAYER_NAME, PLAYER_LOCATION, and PLAYER_INVENTORY. In the base they are "Luke", None, and list The adventure content module uses "from mask_IF import *", changing any of those variables makes it in the current namespace- it doesn't alter the imported ones. And due to *, I can't mask_IF.VARIABLE = value either.

I would like to keep my "from mask_IF import *", but at the same time I have these variables which should be defined with every separate adventure module and are used in the main module. """

==== In the main module:

PLAYER_NAME = "Luke"
...
class ActionHelp(Action):
    def ActionHandle(self, Object1, Object2):
        print "No help for you, " + PLAYER_NAME + "!"

In the adventure module:

from mask_IF import *
Text("Your name is normally " + PLAYER_NAME)
...
PLAYER_NAME = "Adventure_Tester #001"
Text("But today you are " + PLAYER_NAME)

==== Your name is normally Luke But today you are Adventure_Tester #001

help No help for you, Luke! The command is help The Object1 is NOT GIVEN The Object2 is NOT GIVEN

Upvotes: 0

Views: 168

Answers (3)

gahooa
gahooa

Reputation: 137552

If you were to use an object like StrObj instead of strings, then you would be dealing with a mutable type reference that could have a value updated from anywhere. The only difference in your code would be in setting it with a call to Set() rather than = '...'.

class StrObj(object):
    def __init__(self, value=''):
        self.value = value
    def __add__(self, other):
        return other + self.value
    def Set(self, value):
        self.value = value

Assuming that in mask_IF.py, PLAYER_NAME = StrObj('Some Name')...

from mask_IF import *
Text("Your name is normally " + PLAYER_NAME)
...
PLAYER_NAME.Set("Adventure_Tester #001")
Text("But today you are " + PLAYER_NAME)

All that being said...

I think you would be better off defining a Player object with properties for NAME, etc... and just using that instead. I recommend using import * sparsely.

Upvotes: 0

Whatang
Whatang

Reputation: 10376

Yeah, you can't do it. the "from mask_IF import *" is creating a new variable named PLAYER_NAME in your current module, which happens to point to the same underlying data to begin with. It is not an alias for the variable in the mask_IF module. When you try to assign something else to PLAYER_NAME, you're telling the local variable to point to something new, which has no effect at all on what the mask_IF variable points to.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799400

You can't do this, for the reason you have given. It can be simulated by creating an object with the appropriate attributes, but then you may as well not import * to begin with.

Upvotes: 3

Related Questions