[Tutor] self-modification
Kent Johnson
kent37 at tds.net
Mon Aug 21 16:22:52 CEST 2006
Zsiros Levente wrote:
> I'm a newbie to Python. I wanted to write a graphical interface which
> would show the built-in documentation (I mean dir() and __doc__ ) of
> Python. The code shows how far I got before realizing that I had to use
> some self-modification feature (converting a string to a function or to
> an object; i'm not sure you would call it self-modification in an
> interpreted language).
>
You can convert a string to a function or object using exec and eval but
often it's better to use Python's introspection capabilities such as
getattr() and setattr(). For example if you want to convert a string
received from dir() to the value of the corresponding attribute, use
getattr(). Can you say more about why you need to convert a string to an
object?
> I was googling around, and searching in the maillist archives and found
> this:
> http://mail.python.org/pipermail/tutor/2004-October/032388.html
> Unanswered for two years. Not too promising :)
>
That post is pretty vague. I'm surprised that there was no followup
asking for clarification but not surprised that there is no answer.
I don't know of a tutorial on exec and eval but you can find docs here:
http://docs.python.org/ref/exec.html
http://docs.python.org/lib/built-in-funcs.html#l2h-23
> P.S.: I'm new to this mailing list, so I don't know whether it is
> allowed to send code as attachment. (I didn't found anything about your
> policy in the subscription site. ) If not, I'm sorry.
Short code snippets such as yours can be sent as an attachment or inline
in the email. Personally I prefer inline as it makes it easy to reply
with comments interspersed with the code.
Kent
More information about the Tutor
mailing list