[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

object scope

J. Peng

1/21/2008 4:03:00 AM

Please see the code below,what's the scope for object "name"?
I thought it should be located in the while block, but it seems not
really,it can be accessed out of while (the db[name] statement).Thanks
in advance.


db = {}
def newuser():
prompt = 'login desired: '
while 1:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
pwd = raw_input('passwd: ')
db[name] = pwd
4 Answers

Dennis Lee Bieber

1/21/2008 4:31:00 AM

0

On Mon, 21 Jan 2008 12:02:50 +0800, "J. Peng" <jpeng@block.duxieweb.com>
declaimed the following in comp.lang.python:

> Please see the code below,what's the scope for object "name"?

Please repost using a client that doesn't left justify all lines --
your post has lost all indentation, so it is nearly impossible to
determine what constitutes the while block.

> I thought it should be located in the while block, but it seems not
> really,it can be accessed out of while (the db[name] statement).Thanks
> in advance.
>
>
Guessing at the indentation:

> db = {}
> def newuser():
> prompt = 'login desired: '
> while 1:
Any recent Python should take

while True:

> name = raw_input(prompt)
> if db.has_key(name):
> prompt = 'name taken, try another: '
> continue

continue is not needed
> else:
> break

The whole loop can turn into

while True:
name = raw_input(prompt)
if name not in db: break
prompt = "'%s' is in use, choose another: " % name


> pwd = raw_input('passwd: ')
> db[name] = pwd

The scope of "name" is the entire function; lacking a "global name"
statement, AND being on the left side of an assignment, it is a function
local name.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

J. Peng

1/21/2008 5:17:00 AM

0

Dennis Lee Bieber å??é?:
> The scope of "name" is the entire function; lacking a "global name"
> statement, AND being on the left side of an assignment, it is a function
> local name.

Thank you. Does python have so-called 'block scope' object?
or if you can,please show me the doc for python's object scope.

George Sakkis

1/21/2008 5:42:00 AM

0

On Jan 21, 12:16 am, "J. Peng" <jp...@block.duxieweb.com> wrote:

> Dennis Lee Bieber ??:
>
> > The scope of "name" is the entire function; lacking a "global name"
> > statement, AND being on the left side of an assignment, it is a function
> > local name.
>
> Thank you. Does python have so-called 'block scope' object?

No, it doesn't; in most cases that you may care, a name's scope is
either local or global*.

> or if you can,please show me the doc for python's object scope.

http://www.network-theory.co.uk/docs/pytut/PythonScopesandNameS...


George


* There is also class scope and lexical closures but most likely you
don't have to worry about them for now.

Gabriel Genellina

1/21/2008 5:46:00 AM

0

En Mon, 21 Jan 2008 03:16:37 -0200, J. Peng <jpeng@block.duxieweb.com>
escribió:

> Dennis Lee Bieber å??é?:
>> The scope of "name" is the entire function; lacking a "global name"
>> statement, AND being on the left side of an assignment, it is a function
>> local name.
>
> Thank you. Does python have so-called 'block scope' object?
> or if you can,please show me the doc for python's object scope.

See http://docs.python.org/ref/n...

--
Gabriel Genellina