The Artima Developer Community
Sponsored Link

Python Answers Forum
python this var, who joke

1 reply on 1 page. Most recent reply: Jan 17, 2007 10:21 PM by Christophe

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
qinxian xiang

Posts: 5
Nickname: moonlight
Registered: Dec, 2006

python this var, who joke Posted: Dec 1, 2006 12:18 PM
Reply to this message Reply
Advertisement
Hi, check out this code please:

# Filename: vartest.py

import os
path='/tmp/'

def hi():
path = os.path.join(path, 'hi')
print path

hi()


I got a error when run python.
Traceback (most recent call last):
File "vartest0.py", line 8, in ?
hi()
File "vartest0.py", line 5, in hi
path = os.path.join(path, 'hi')
UnboundLocalError: local variable 'path' referenced before assignment

I wanna ask, who just do joke? python or me?

If python:
print 'python joke a not jokable joke.'
else:
print 'I wanna learn more python.'


Christophe

Posts: 3
Nickname: mccricri
Registered: Jan, 2004

Re: python this var, who joke Posted: Jan 17, 2007 10:21 PM
Reply to this message Reply
I think it's you ;)

In your function hi(), the variable path is a new variable, only visible in this function.
So when python say that you try to used it before assignement, it is right for the function visibility.

You have to say that you want use the global variable that has the same name :

def hi():
global path
path = os.path.join(path,"hi")
print path

But with this, the global variable is change every call to the hi() function.

Flat View: This topic has 1 reply on 1 page
Topic: How to call a class where it's name passed from variable Previous Topic   Next Topic Topic: Python + SQLALchemy under WIndows

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use