Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-28-2004, 06:25 PM   #1
Registered: Sep 2003
Distribution: Slackware 9.1 with fluxbox
Posts: 259

Rep: Reputation: 30
simple python function question

Hey, i am a python newb, so dont laugh

Say i have

contents = ['rat' 'dennis']
and later on..

def look(object=None): 
        if len(contents) > 0:
I get a global error, contents is not defined.

How can I access the contents list from inside a function?
Old 03-28-2004, 07:25 PM   #2
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
You have crossed into a second "namespace". Named variables don't automatic'ly cross over into the function. In the function, it's looking for the variable contents, but it hasn't been defined in the function. You can have the same name inside and outside the function, with different values.

You can add "global contents" as the first line of the def, and it will use the main variable named contents. Or, you can pass the variable along when you call the function. You are already passing the variable object along, just add in contents.
Old 03-28-2004, 10:53 PM   #3
Registered: Sep 2003
Distribution: Slackware 9.1 with fluxbox
Posts: 259

Original Poster
Rep: Reputation: 30
thanks.. i ended up putting the class name before contents, that works too!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Bash function mijohnst Programming 13 10-23-2005 12:40 PM
Python function defaults behaving badly DiagonalArg Programming 3 08-24-2005 11:35 AM
how do I do this in Python? (simple sh script) johnMG Programming 6 12-22-2004 10:05 PM
python call function in same file(very newbie question) rmanocha Programming 2 11-21-2004 12:04 AM
Python function arguments are dictionaries? johnMG Programming 1 07-24-2004 06:29 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration