names of parameters as string

Jean Brouwers JBrouwersAtProphICyDotCom at no.spam.net
Sat Oct 30 03:08:50 EDT 2004


The simplest thing to do would be to make the variable an attribute of
a class instance.

  class Vars: pass
   # create one instance
  v = Vars()
   # add vars as needed
  v.text = "text"
  v.int = 12
  ...
  print v.text, v.int

Is this what you need?

/Jean Brouwers
 ProphICy Semiconductor, Inc.


In article <mailman.5701.1099119087.5135.python-list at python.org>,
andrea valle <andrea.valle at unito.it> wrote:

> Thanks to all for the replies.
> Now, sorry, something related.
> Suppose I have a string and I'd like to create a variable with its name.
> How can I do that?
> 
> I tried to create the variable text assigning to it the value "text" 
> with this:
>  >>> eval("text = \"text\"")
> 
> But it raises an error.
> 
> Thanks as usual
> 
> Best
> 
> -a-
> 
> 
> 
> 
> On 30 Oct 2004, at 05:59, Jean Brouwers wrote:
> 
> >
> >
> > Take a look at the getargspec() function in the inspect module.  It
> > returns a 4-tuple and the first item of that tuple is a list of the
> > parameter names of a function or method.
> >
> > <pre>
> >   import inspect
> >   def foo(p1, p2):
> >     pass
> >   print inspect.getargspec(foo)
> >
> > results in:
> >
> >   (['p1', 'p2'], None, None, None)
> >
> > </pre>
> >
> > The second item is the name of the *args argument, the third is the
> > name of the **kwds argument and the last item is a tuple of the values
> > of the kwds args.
> >
> > More details in
> >
> > <http://docs.python.org/lib/inspect-classes-functions.html>
> >
> > and on page 381 of the (outstanding, IMO) book "Python in a Nutshell",
> > 1st edition.
> >
> >
> > /Jean Brouwers
> >  ProphICy Semiconductor, Inc.
> >
> >
> >>>>> def foo( foo_param1, foo_param2):
> >
> >
> > In article <mailman.5663.1099065452.5135.python-list at python.org>,
> > andrea valle <andrea.valle at unito.it> wrote:
> >
> >> Hi to all,
> >> I'd like to access the parameter list of a function (or better of a
> >> method) in order to generate GUI elements.
> >> That is.
> >> If I have this:
> >>
> >>>>> def foo( foo_param1, foo_param2):
> >>     print foo_param
> >>
> >> I'd like to have something like:
> >>
> >>>>> print foo.param_list
> >>>>> 'foo_param1', 'foo_param2'
> >>
> >> My idea is to use the foo_params strings in Tkinter as text for Label
> >> and as variables for Entries.
> >>
> >> What have I to do?
> >>
> >> Thanks a lot as usual
> >>
> >> Best
> >>
> >> -a-
> >>
> >>
> >> _____________________________________________________________________
> >> For your security, this mail has been scanned and protected by Inflex
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
> >
> Andrea Valle
> Laboratorio multimediale "G. Quazza"
> Facoltà di Scienze della Formazione
> Università degli Studi di Torino
> andrea.valle at unito.it
>



More information about the Python-list mailing list