473,394 Members | 1,875 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,394 software developers and data experts.

Global name "userid" not defined??

How do I rectify this?

------vars.py------

#!/usr/local/bin/python

def setUserid(value):
userid = value

def getUserid():
return userid

--------------------
import vars
vars.setUserid("Sean")
vars.getUserid()


Then I get a Traceback error and the last line says:
NameError: name 'userid' is not defined

How do I make a variable global?

Sorry about the easy question... but I don't have all that much experience
in Python.
Jul 18 '05 #1
3 11880
Sean Berry wrote:
#!/usr/local/bin/python

def setUserid(value):
userid = value

def getUserid():
return userid

How do I make a variable global?


You need to use the "global" keyword (check the docs for more
information) with the name of the variable at the top of each
function which wants to *modify* the global variable (including by
creating it in the first place).

Although you don't need to include this ("global userid") in
the second function, it would probably be considered "good style".
Of course, using globals at all is pretty bad style, but as you're
just learning it's not the worst thing to do. (Close, but not
the worst... just consider learning how to avoid them, generally
by making things more object-oriented.)

-Peter
Jul 18 '05 #2
In message <Uqiac.1044$zh.796@fed1read07>, Sean Berry wrote:
How do I rectify this? def setUserid(value): global userid userid = value

def getUserid(): global userid return userid


--
Garry Knight
ga*********@gmx.net ICQ 126351135
Linux registered user 182025
Jul 18 '05 #3
On Tue, 30 Mar 2004 09:53:47 -0600, Sean Berry wrote:
How do I make a variable global?


Mu. (You're asking the wrong question.)

Why do you need to make a variable global? Global variables are bad
style in almost every instance; an object to manage the variable gives
you readability and protection from unexpected side-effects.

If you explain what it is you want to achieve with a global variable,
it's likely a better solution can be proposed.

--
\ "Giving every man a vote has no more made men wise and free |
`\ than Christianity has made them good." -- Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #4

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

Similar topics

8
by: TWACS | last post by:
How do I add and get session defined variables? when I do this Session("USERID") = "NAME"; I get System.Web.UI.Page.Session' denotes a 'property' where a 'method' was expected Thanks,
2
by: Ferd Biffle | last post by:
Hello! I have an Excel XML feed from an Enterprise PDM system that is basically a flat text file with XML tags: <?xml version="1.0" encoding="UTF-8"?> <Import> <Row> <ID>1</ID>...
2
by: mike | last post by:
I know that I can create a form element using: <xsl:element name="input"> but what about another tag like <td>. <xsl:element name="td"> does not work. Mike
0
by: mc kim via .NET 247 | last post by:
What I am trying to is automatic bidding process in Ebay site... WebClient class can uploads form data easily. I have collected ebay url & form data to uploads.. Most of process is quite easy...
3
by: Peter Theill | last post by:
Hi, I'm trying to include the content of a request into an Xml document: if (System.Web.HttpContext.Current.Session != null) { XmlDocument d = new XmlDocument(); d.Load(new...
9
by: Patrick | last post by:
I have an ASP.NET page that searches for someone in the corporate Active Directory. It had been working fine until recently when I changed from Basic Authentication on IIS6 back to Integrated...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
3
by: kang jia | last post by:
hi i am doing reply page for forum, users can reply for the specific topic they would like to reply. Firstly of all, when click the topic name in topic page. i will redirect them to reply page....
0
by: vaibhavsumant | last post by:
<project name="DBCreate" default="usage" basedir="."> <property name="user" value="db2admin"/> <property name="passwd" value="db2admin"/> <property name="dbprefix" value=""/> <property...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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,...

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.