473,804 Members | 3,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,v ar1,var2,var3):
self.var1 = var1
self.var2 = var2
Jul 3 '06 #1
4 1435
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,v ar1,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,va r2,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***********@a cm.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,v ar1,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,va r2,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***********@a cm.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
2669
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 too "object"-ified. Anyways, I've created a wrapper class for MySQL connectivity. It's barebones for now, no error checking or anything. So now I'm trying to create user/session-handling class that would work with the data stored in the database...
15
1928
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 handle all database functions. In this class I try to recover the var's from my config file,
8
6109
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
3
3359
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
24189
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(); function loadUrlVariables() { varString = location.search;
45
3632
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
3501
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 which each page will use, but my question relates to how to use the database connection i create in all my classes. I have a database class, in a separate namespace and file, i created that handles all the connection opening, executing statements...
6
2568
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 variables improve data sharing between SQL statements. Version 9.5 introduces the concept of global variables, which are named memory variables that you can access and modify through SQL statements. Global variables enable you to share data between...
12
11119
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.