Classes and global statements

Sheldon shejo284 at gmail.com
Mon Jul 3 10:54:05 EDT 2006


Hi,

I have a series of classes that are all within the same file. Each is
called at different times by the main script. Now I have discovered
that I need several variables returned to the main script. Simple,
right? I thought so and simply returned the variables in a tuple:
(a,b,c,d,e) = obj.method()
Now I keep getting this error: "ValueError: unpack tuple of wrong size"


 I think this is because I am trying to return some Numeric arrays as
well as list and I didn't declare these prior to calling the class
method. The problem is that some of these arrays are set within the
class and cannot be set in the calling script. I removed these arrays
and tried it again and still I get this error. So I have another idea:

I have one class that sets a bunch of varibles like this:
myclass:
  def __init__(self,var1,var2,var3):
         self.var1 = var1
         self.var2 = var2
         .
         .
         .
         etc.
Then I use the following script to make these variable global:

global main
main = myclass(var1,var2,var3)

I am thinking that I should be able to "insert" other variable into
this main from within other classes like this:

otherclass:
 def __init__(self,a,b,c,d):
       self.a = a..... etc.
 def somemethod(self):
       self.newvar = ........
       main.newvar = self.newvar
       return self.a
****************************************
This looks wierd but I am wondering if it will work? This would be a
wonderful way to store variables that will be needed later instead of
passing them back and forth.
After trying this it did work! My question is why? Another way to solve
this problem is to make the variable I need global in the class that
they are created. Does anyone have a better way in mind?

Sincerely,
Sheldon




More information about the Python-list mailing list