Accessing variables created after import in imported files 
Author Message
 Accessing variables created after import in imported files

Sorry about the somewhat unclear subject line.  I am not totally sure
of the general form of my problem, so it is hard to write it as a
subject line.  My problem involves a program I am writting to allow
people to play a really fun card game I know, Hot Death Uno.  Insted of
posting the actual code(It's really long, and even snipits would be
pretty complex) I have enclosed code which demostrates the problem, but
is not the actual code I am working with.  If someone want's to see it,
I would be glad to e-mail it to them.
        The code is in two files:
#file 1
import file2

class Game:
    def __init__(self):
        global TheGame
        TheGame=self
        #this lets other classes access the current
        #instence of the game without knowing the name it was
        #created as.
        self.foo='Some random variable'
x=Game()
foo=file2.ThingThatUsesGameClass()

#file 2
import file1

class ThingThatUsesGameClass:
    def __init__(self):
        print TheGame.foo #this should use the instance of the Game     #class
created in the other file.

                Thank you very much for your time,
                                        Jesse Weinstein



Fri, 20 Jun 2003 11:40:03 GMT  
 Accessing variables created after import in imported files
Try:
    ...
    #file 2
    import file1

    class ThingThatUsesGameClass:
       def __init__(self):
           print file1.TheGame.foo #this should use the instance of the Game
#class
    ...

Quote:

> Sorry about the somewhat unclear subject line.  I am not totally sure
> of the general form of my problem, so it is hard to write it as a
> subject line.  My problem involves a program I am writting to allow
> people to play a really fun card game I know, Hot Death Uno.  Insted of
> posting the actual code(It's really long, and even snipits would be
> pretty complex) I have enclosed code which demostrates the problem, but
> is not the actual code I am working with.  If someone want's to see it,
> I would be glad to e-mail it to them.
> The code is in two files:
> #file 1
> import file2

> class Game:
>     def __init__(self):
> global TheGame
> TheGame=self
> #this lets other classes access the current
>   #instence of the game without knowing the name it was
> #created as.
> self.foo='Some random variable'
> x=Game()
> foo=file2.ThingThatUsesGameClass()

> #file 2
> import file1

> class ThingThatUsesGameClass:
>     def __init__(self):
> print TheGame.foo #this should use the instance of the Game #class
> created in the other file.

> Thank you very much for your time,
> Jesse Weinstein



Fri, 20 Jun 2003 13:56:05 GMT  
 Accessing variables created after import in imported files
Dear Scott,
        Thank you very much, your solution works quite well.  I wonder why I
did not think of it myself, or why I was so folish as to reject it when
I had it.  But, now I am faced with another strange and puzzinling
probblem; however, this one I have found a solution for, but I have no
idea _why_ the solution works.  When I run the code I wrote up, with
your change, it gives an AttributeError on the use of
ThingThatUsesGameClass in file1.  However, if you reload file2 after
importing it, then the code works perfectly.  Why?  How?  This makes no
sense!  As far as I knew, importing and reloading were identical, as
long as the file was unchanged.  Do you have any ideas about why this
could be?

                                        Thank you for your solution,
                                                        Jesse W

Quote:

> Try:
>     ...
>     #file 2
>     import file1

>     class ThingThatUsesGameClass:
>        def __init__(self):
>            print file1.TheGame.foo #this should use the instance of the Game
> #class
>     ...



Fri, 20 Jun 2003 16:12:55 GMT  
 Accessing variables created after import in imported files
Hi Jesse

My take on this is that you have a mutual dependency to remove.
One possible solution:  Don't let file1 and file2 import each other.

Create file3:
import file1, file2

TheGame=file1.Game()
foo = file2.ThingThatUsesGameClass(TheGame)

--Darrell

Quote:

> Dear Scott,
> Thank you very much, your solution works quite well.  I wonder why I
> did not think of it myself, or why I was so folish as to reject it when
> I had it.  But, now I am faced with another strange and puzzinling
> probblem; however, this one I have found a solution for, but I have no
> idea _why_ the solution works.  When I run the code I wrote up, with
> your change, it gives an AttributeError on the use of
> ThingThatUsesGameClass in file1.  However, if you reload file2 after
> importing it, then the code works perfectly.  Why?  How?  This makes no
> sense!  As far as I knew, importing and reloading were identical, as
> long as the file was unchanged.  Do you have any ideas about why this
> could be?

> Thank you for your solution,
> Jesse W



Fri, 20 Jun 2003 22:53:31 GMT  
 Accessing variables created after import in imported files
Dear Darrell,
        Thank you.  If I seperate the executing code from the definitions, it
works.  But what is a mutual dependency, and why should that cause a
class to not appear when it is first imported?
                                                Thank you,
                                                        Jesse Weinstein
Quote:

> Hi Jesse

> My take on this is that you have a mutual dependency to remove.
> One possible solution:  Don't let file1 and file2 import each other.

> Create file3:
> import file1, file2

> TheGame=file1.Game()
> foo = file2.ThingThatUsesGameClass(TheGame)

> --Darrell


> > But, now I am faced with another strange and puzzinling
> > probblem; however, this one I have found a solution for, but I have no
> > idea _why_ the solution works.  When I run the code I wrote up, with
> > your change, it gives an AttributeError on the use of
> > ThingThatUsesGameClass in file1.  However, if you reload file2 after
> > importing it, then the code works perfectly.  Why?  How?  This makes no
> > sense!  As far as I knew, importing and reloading were identical, as
> > long as the file was unchanged.  Do you have any ideas about why this
> > could be?

> > Thank you for your solution,
> > Jesse W



Sat, 21 Jun 2003 02:44:03 GMT  
 Accessing variables created after import in imported files

Quote:

> Thank you.  If I seperate the executing code from the definitions, it
> works.  But what is a mutual dependency, and why should that cause a
> class to not appear when it is first imported?

Python *executes* module code to fill in the module name-
space.  if you do recursive imports, you may end up trying
to access objects that hasn't been created yet.

for more info, see the section "What Does python Do to
Import a Module" in

    http://effbot.org/guides/import-confusion.htm

</F>



Sat, 21 Jun 2003 03:38:29 GMT  
 Accessing variables created after import in imported files
Hi Jesse,

I think you would have a better design if you passed the Game instance
as a parameter to the ThingThatUsesGame constructor.  It seems to make
sense (to me anyway) that you would want to tell the ThingThatUsesGame
which game he's using. Of course, I'm not familiar with your application
so there may be other issues that require you to use a global, but, in
general, globals should be avoided when ever possible.

class ThingThatUsesGame:
    def __init__(self, game):
        self.game = game
        print self.game.foo

gameX = Game()
gameUserX = ThingThatUsesGame(gameX)

# or, if you don't need to refer to the game instance anywhere else:

gameUserX = ThingThatUsesGame(Game())

This design will also allow you to have multiple users playing
different games.

- Don

Quote:

> Sorry about the somewhat unclear subject line.  I am not totally sure
> of the general form of my problem, so it is hard to write it as a
> subject line.  My problem involves a program I am writting to allow
> people to play a really fun card game I know, Hot Death Uno.  Insted of
> posting the actual code(It's really long, and even snipits would be
> pretty complex) I have enclosed code which demostrates the problem, but
> is not the actual code I am working with.  If someone want's to see it,
> I would be glad to e-mail it to them.
>         The code is in two files:
> #file 1
> import file2

> class Game:
>     def __init__(self):
>         global TheGame
>         TheGame=self
>         #this lets other classes access the current
>         #instence of the game without knowing the name it was
>         #created as.
>         self.foo='Some random variable'
> x=Game()
> foo=file2.ThingThatUsesGameClass()

> #file 2
> import file1

> class ThingThatUsesGameClass:
>     def __init__(self):
>         print TheGame.foo #this should use the instance of the Game     #class
> created in the other file.

>                 Thank you very much for your time,
>                                         Jesse Weinstein



Sat, 21 Jun 2003 16:18:08 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Importing via Access 97 odbc driver variable length Access Memo fiels

2. import Image vs from PIL import Image vs import PIL.Image

3. Import Data With File Import

4. Importing MS Access files (ODBC)

5. Importing Clipper files in to MS Access

6. Import from .tps files to SQL Server or Access

7. Import text file into access database by active x

8. Join clipper files with ACCESS. NOT import

9. comp3-ANSI-file => MS Access Import

10. Import dos file variable record length

11. Super Import/Import Template from Mitten

12. IMPORT IO, FROM IO IMPORT ?

 

 
Powered by phpBB® Forum Software