Classes and global statements

S

Sheldon

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
 
S

Simon Forman

Sheldon said:
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
 
S

Scott David Daniels

Sheldon said:
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"
What you need to do at this point is read the _whole_ error message
(including the ugly traceback stuff). Think hard about what is going
on. "ValueError: unpack tuple of wrong size" comes from returning
a tuple of a different size than your assignment is producing:

def triple(v):
return v, v*v

x, y, z = triple(13)

Break up the assignment and insert prints:

# x, y, z = triple(13)
temp = triple(13)
print "I'm going to expand the triple:", temp
x, y, z = temp

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.
Don't guess and hypothesize; create experiments and test. Your
reasoning is off in the ozone. You waste a lot of time creating
great theories; make tiny theories and _test_ them.

<<and the theories wander farther and farther off into the weeds>>

--Scott David Daniels
(e-mail address removed)
 
S

Sheldon

Simon Forman skrev:
Sheldon said:
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
 
S

Sheldon

Scott David Daniels skrev:
What you need to do at this point is read the _whole_ error message
(including the ugly traceback stuff). Think hard about what is going
on. "ValueError: unpack tuple of wrong size" comes from returning
a tuple of a different size than your assignment is producing:

def triple(v):
return v, v*v

x, y, z = triple(13)

Break up the assignment and insert prints:

# x, y, z = triple(13)
temp = triple(13)
print "I'm going to expand the triple:", temp
x, y, z = temp


Don't guess and hypothesize; create experiments and test. Your
reasoning is off in the ozone. You waste a lot of time creating
great theories; make tiny theories and _test_ them.

<<and the theories wander farther and farther off into the weeds>>

--Scott David Daniels
(e-mail address removed)



Thanks for sound advice!
Will give it another go.

/Sheldon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top