SyntaxError: 'return' outside function

M

Melih Onvural

Has anyone seen this error before and been able to solve it? I can't
seem to find anything that leads to a solution. I found this post
http://zope.org/Collectors/Zope/1809, but can't really understand it.
I've attached my code below to see if anything looks funny. It happens
at the very last return at the end. Thanks in advance,

--melih

===========Code========

def legiturl(self, url):
# this breaks down the url into 6 components to make sure it's
"legit"
t = urlparse.urlparse(url)

if t[0] != 'http':
return ""

# remove URL fragments, but not URL
if len(t[5]) > 0:
url = urlparse.urlunparse((t[0],t[1],t[2],"","",""))
t = urlparse.urlparse(url)

# stupid parser sometimes leaves frag in path
x = find(t[2], '#')
if x >= 0:
return ""
 
S

Steven Bethard

Melih said:
Has anyone seen this error before and been able to solve it? I can't
seem to find anything that leads to a solution. I found this post
http://zope.org/Collectors/Zope/1809, but can't really understand it.
I've attached my code below to see if anything looks funny. It happens
at the very last return at the end. Thanks in advance,

--melih

===========Code========

def legiturl(self, url):
# this breaks down the url into 6 components to make sure it's
"legit"
t = urlparse.urlparse(url)

if t[0] != 'http':
return ""

# remove URL fragments, but not URL
if len(t[5]) > 0:
url = urlparse.urlunparse((t[0],t[1],t[2],"","",""))
t = urlparse.urlparse(url)

# stupid parser sometimes leaves frag in path
x = find(t[2], '#')
if x >= 0:
return ""

Looks like you're mixing tabs and spaces. Your "return" statement is
outside the "legiturl" function.

Steve
 
S

skip

Melih> Has anyone seen this error before and been able to solve it? I
Melih> can't seem to find anything that leads to a solution.

Your code is incorrectly indented. Try:

def legiturl(self, url):
# this breaks down the url into 6 components to make sure it's "legit"
t = urlparse.urlparse(url)

if t[0] != 'http':
return ""

# remove URL fragments, but not URL
if len(t[5]) > 0:
url = urlparse.urlunparse((t[0],t[1],t[2],"","",""))
t = urlparse.urlparse(url)

# stupid parser sometimes leaves frag in path
x = find(t[2], '#')
if x >= 0:
return ""

instead. Note also the lack of a return at the end of the function.

Skip
 
M

Melih Onvural

Thanks all, I did a massive make sure everything is indents and not
spaces across all of my files and now things are running much more
smoothly. I appreciate the responses.

--melih
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top