[János Juhász]

| I can't find the defined() function in python, so I used 
|         
| 'variable name' in dir() 
| 
| for check if the variable defined. 
| 
| >>> name = 'Joe' 
| >>> if 'name' in dir(): 
| ...         print name 
| ...         

I'm not entirely sure where you'd want
to use this, but probably the most
Pythonic way of doing this would be:

<code>
name = "Joe"
try:
  name
except NameError:
  print "name not defined"
else:
  print "name defined"
</code>

I suspect that your idea of variable definition
doesn't quite match Python's concept. In short,
it's impossible to "declare" a variable in Python
without binding it to *something*. ie a variable
is always a binding to an object, not a hole
waiting to be filled.

You could, if you wanted, initialise name to None
(or some other sentinel value) and then check
against that, either explicitly:

if name is None:
  print "name unitialised"

or by taking advantage of the fact that several
empty objects in Python are considered False:

if not Name:
  print "name unitialised"

Hope that helps more than it confuses.
TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to