473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Classes and global statements

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
Jul 3 '06 #1
4 1414
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

Jul 3 '06 #2
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"
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
sc***********@acm.org
Jul 3 '06 #3

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

Jul 3 '06 #4

Scott David Daniels 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"
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
sc***********@acm.org


Thanks for sound advice!
Will give it another go.

/Sheldon

Jul 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Swartz | last post by:
Hi all. I'm working here on a small project of mine. I'm not new to programming, but I'm new to PHP. You have to understand that I'm coming from C++, OOP world, so my code might seems a little...
15
by: wEEdpEckEr | last post by:
Hi, here's the deal: I have a config.php file in which I have set a few var's, to use in the whole site. E.g.: $db_host = "localhost"; Then I also have a class, which is supposed to...
8
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
3
by: Lord Merlin | last post by:
When using "if then" statements in global, I get the following error: a.. Error Type: (0x8002802B) Element not found. //global.asa, line 12 Can I use if then statements in global?
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
6
by: Frank Swarbrick | last post by:
Interesting! I was going to ask if such a thing existed, but I was pretty much convinced they did not so I didn't ask. Looks like with version 9.5 DB2 supports global variables: "Global...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.