Quoted identifiers in Python?

Eddie and Babs kca17 at dial.pipex.com
Thu Mar 29 08:22:10 EST 2001


While reading an article about the Dylan language a few years ago, I was 
surprised to find that you could have quoted identifiers (ie, variable and
function names, etc). So you could have a function named "turn around" (with
quotes) and then call it using something like:

    "turn around"(xxx)

The idea is that you can put any characters within the quotes.

NOW... would this be a good thing for Python to have in the future? It would
make element access consistent with subscripting:-

        x = obj."Ivor the engine"

            ...would be the same as

        x = obj["Ivor the engine"]

..no?

But the particular advantage would be with __getattr__ (ie, defining
attribute access for a class). It seems that the most obvious use of
__getattr__ is to allow Python-esque access to a dynamic system external to
Python. For example, you could have a class which allowed access to the file
system via Python objects:-

    import filesys

    # MyHD is a hard disk which contains the path "MyHD/projects/web/index".

    fileObj = FileSys()

    textOfFile = fileObj.MyHD.projects.web.index.readAll()


...etc. This falls down (slightly) when the file is called "index.htm", as
the dot is interpreted incorrectly by Python. What you need to be able to
say is:-

    textOfFile = fileObj.MyHD.projects.web."index.htm".readAll()

You could of course use the subscripting notation
(MyHD.projects.web["index.htm"]) but this seems to disrupt the flow
somewhat.


Maybe this idea is flawed in ways I can't see, but I thought it was at least
worth mentioning.


Cheers,
&.




More information about the Python-list mailing list