Re: Printing a variable's name not its value

2005-07-20 Thread travislspencer
[EMAIL PROTECTED] wrote:
> Bruno Desthuilliers wrote:
> > def printVerbose(*args, **kwargs):
> >
> > > if VERBOSE in globals:
> > > for a in args:
> > > if a in globals:
> > > value = globals[a]
> >
  for k, v in kwargs.iteritems():
> > > print "%s: %s" % (k, v)
> > >
>
> Perfect.  Thanks for the pointer.

Well, almost perfect ;)

--

Regards,

Travis Spencer

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a variable's name not its value

2005-07-20 Thread tiissa
Simon Dahlbacka wrote:
> as you have been told, there is no way to get a variable's name

Well, if you really want to, you can get all the names bound to a given 
object:


def get_names(obj):
 g = globals()
 names = []
 for name in g:
 if g[name] is obj:
 names.append(name)
 return names


Then you can play around:

  >>> list1 = []
  >>> list2 = [list1]
  >>> get_names(list2)
  ['list2']
  >>> list3 = list2
  >>> get_names(list2)
  ['list3', 'list2']
  >>> get_names(1)
  []
  >>> a = 1
  >>> get_names(1)
  ['a']
  >>> b = 1
  >>> get_names(1)
  ['a', 'b']
  >>> get_names(a)
  ['a', 'b']
  >>> c = 4/3.
  >>> d = 4/3.
  >>> get_names(c)
  ['c']
  >>> get_names(d)
  ['d']
  >>> get_names(4/3.)
  []
  >>>


But I wouldn't do it. If I want a name to be attached to some objects, I 
usually include it as a member/property of my class.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a variable's name not its value

2005-07-20 Thread travislspencer
Simon Dahlbacka wrote:
> as you have been told, there is no way to get a variable's name, take a
> look at http://effbot.org/zone/python-objects.htm to find out why this
> is so.

Thanks, Simon, for the link.

--

Regards,

Travis Spencer

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a variable's name not its value

2005-07-20 Thread travislspencer
Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] a écrit :
> > globals = {}
>
> globals() is a builtin function, you should no shadow it.

Oh, woops.  I'll fix that.

> def printVerbose(*args, **kwargs):
>
> > if VERBOSE in globals:
> > for a in args:
> > if a in globals:
> > value = globals[a]
>
>   for k, v in kwargs:
> > print "%s: %s" % (k, v)
> >

Perfect.  Thanks for the pointer.

> > I've been told on #python that there isn't a way to get a variable's
> > name.  I hope this isn't so.
>
> It is so. In fact, there is nothing like a 'variable' in Python. What we
> have are names bound to objects. Names 'knows' what objects are bound to
> them, but objects knows *nothing* about names they are bound to.

OK.  This seems like it might take some getting used to.  Thanks again
for the help.

--

Regards,

Travis Spencer

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a variable's name not its value

2005-07-20 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Hey,
> 
> I am trying to write a function that takes an arbitrary number of
> arguments and does one of two things.  If the variable is a key in a
> dictionary, it prints the key and its value.  Otherwise, if any of the
> variables isn't in the dictionary, the function prints the variable's
> name and value.
> 
> Here is what I have so far:
> 
> globals = {}

globals() is a builtin function, you should no shadow it.

> HOME_DIR = "The user's home directory"
> SHELL = "The user's shell"
> 
> def someFunction():
> someString = "This is a test"
> globals[VERBOSE] = True
> globals[HOME_DIR] = os.getenv("HOME")
> globals[SHELL] = os.getenv("SHELL")
> 
> printVerbose(someString, HOME_DIR, SHELL)

-> printVerbose(HOME_DIR, SHELL, someString=someString)

> def printVerbose(*args):
def printVerbose(*args, **kwargs):

> if VERBOSE in globals:
> for a in args:
> if a in globals:
> value = globals[a]

  for k, v in kwargs:
> print "%s: %s" % (k, v)
> 

(snip)

> I've been told on #python that there isn't a way to get a variable's
> name.  I hope this isn't so.  

It is so. In fact, there is nothing like a 'variable' in Python. What we 
have are names bound to objects. Names 'knows' what objects are bound to 
them, but objects knows *nothing* about names they are bound to.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a variable's name not its value

2005-07-20 Thread Simon Dahlbacka
as you have been told, there is no way to get a variable's name, take a
look at http://effbot.org/zone/python-objects.htm to find out why this
is so.

-- 
http://mail.python.org/mailman/listinfo/python-list


Printing a variable's name not its value

2005-07-20 Thread travislspencer
Hey,

I am trying to write a function that takes an arbitrary number of
arguments and does one of two things.  If the variable is a key in a
dictionary, it prints the key and its value.  Otherwise, if any of the
variables isn't in the dictionary, the function prints the variable's
name and value.

Here is what I have so far:

globals = {}
HOME_DIR = "The user's home directory"
SHELL = "The user's shell"

def someFunction():
someString = "This is a test"
globals[VERBOSE] = True
globals[HOME_DIR] = os.getenv("HOME")
globals[SHELL] = os.getenv("SHELL")

printVerbose(someString, HOME_DIR, SHELL)

def printVerbose(*args):
if VERBOSE in globals:
for a in args:
value = ""

if a in globals:
value = globals[a]
#else
#a = a.__name__
#value = a

print "%s: %s" % (a, value)

This prints something like this:

The user's home directory: /home/tspencer
The use's shell: zsh

But what I would like it to print is this:

The user's home directory: /home/tspencer
The use's shell: zsh
someString: This is a test

I've been told on #python that there isn't a way to get a variable's
name.  I hope this isn't so.  If it helps, I am trying to do something
like what the C preprocessor's # operator does.

TIA.

--

Regards,

Travis Spencer

-- 
http://mail.python.org/mailman/listinfo/python-list