Classes and global statements

Sheldon shejo284 at gmail.com
Mon Jul 3 15:28:33 EDT 2006


Simon Forman skrev:

> Sheldon wrote:
> > 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"
> >
>
> You're likely getting that error because the method() call is returning
> more or less than 5 values..  Try something like this to check:
>
> results = obj.method()
> print results
> a, b, c, d, e = results
>
> That way you'll be able to see exactly what the method is returning.
>
> >
> >  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)
>
> In this case, where the main var is being set at the "module level",
> you needn't use the global statement.  If I understand it correctly,
> global is for use inside functions and methods to indicate that a
> variable is being reused from the outer "global" scope, rather than
> being temporarily "shadowed" by a var of the same name local to the
> function or method.
>
> > 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.
>
> You *can* use an object as a convenient, if unusual, storage place for
> variables because you can assign to attributes after object
> instantiation
>
> >>> class foo: pass
> ...
> >>> bar = foo()
> >>> bar.a = 23
> >>> bar.b = 'Hi there'
> >>> dir(bar)
> ['__doc__', '__module__', 'a', 'b']
> >>> bar.__dict__
> {'a': 23, 'b': 'Hi there'}
>
> There's nothing really wrong with this, it's like using a dict except
> that you can access variables using the attribute notation  obj.var
>
> But keep in mind, if you're already passing around your 'main' object,
> then you already know how to use and pass around any other object.
>
>
> > 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?
>
> I'm not sure, but I think there's no such thing as "global to a class",
>  although you can make class attributes just like for objects.  There
> are some complexities to doing this though so you should probably stick
> to instance objects rather than mucking about with class attributes..
>
> HTH,
> ~Simon

Thanks for sound advice!
Will give it another go.

/Sheldon




More information about the Python-list mailing list