Quoted identifiers in Python? 
Author Message
 Quoted identifiers in Python?

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,
&.



Mon, 15 Sep 2003 21:22:10 GMT  
 Quoted identifiers in Python?

Quote:

> 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?

I would hope they would not be the same, since one invokes __getattr__
while the other invokes __getitem__.  They're different things, they
shouldn't be forced to be the same thing.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Traveler, there are no paths.  Paths are made by walking.
\__/ Antonio Machado
    REALpolitik / http://www.realpolitik.com/
 Get your own customized newsfeed online in realtime ... for free!



Tue, 16 Sep 2003 00:02:12 GMT  
 Quoted identifiers in Python?
Ahem... sorry, I missed the crucial word out there: "__dict__"!
I meant that

    x = obj."Ivor the engine"

...would be the same as:-

    x = obj.__dict__["Ivor the engine"]

The point I was really making was that since the object's attributes are
stored in a dictionary, there is no reason why you shouldn't have any string
you like as the key (you can, after all, do that with any other dictionary).
The ability to use arbitrary strings as attribute names (as opposed to just
the usual alpha-num-underscore characters) DOES have its uses when you are
allowed to re-define the attribute access "dot" operator.

----------

Quote:


>> 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?

> I would hope they would not be the same, since one invokes __getattr__
> while the other invokes __getitem__.  They're different things, they
> shouldn't be forced to be the same thing.

> --

>  __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
> /  \ Traveler, there are no paths.  Paths are made by walking.
> \__/ Antonio Machado
>     REALpolitik / http://www.realpolitik.com/
>  Get your own customized newsfeed online in realtime ... for free!



Tue, 16 Sep 2003 02:50:34 GMT  
 Quoted identifiers in Python?
Well, how would this work?

a = ."Ivory the engine"
ax = obi.a

What is ax?

--

Emile van Sebille

---------


Quote:
> Ahem... sorry, I missed the crucial word out there: "__dict__"!
> I meant that

>     x = obj."Ivor the engine"

> ...would be the same as:-

>     x = obj.__dict__["Ivor the engine"]

> The point I was really making was that since the object's attributes are
> stored in a dictionary, there is no reason why you shouldn't have any
string
> you like as the key (you can, after all, do that with any other
dictionary).
> The ability to use arbitrary strings as attribute names (as opposed to
just
> the usual alpha-num-underscore characters) DOES have its uses when you are
> allowed to re-define the attribute access "dot" operator.

> ----------



> >> 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?

> > I would hope they would not be the same, since one invokes __getattr__
> > while the other invokes __getitem__.  They're different things, they
> > shouldn't be forced to be the same thing.

> > --

> >  __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
> > /  \ Traveler, there are no paths.  Paths are made by walking.
> > \__/ Antonio Machado
> >     REALpolitik / http://www.realpolitik.com/
> >  Get your own customized newsfeed online in realtime ... for free!



Tue, 16 Sep 2003 04:08:02 GMT  
 Quoted identifiers in Python?
[Note: post appeared as edited by my spell checker ;-)]

a = "Ivor the engine"
x = obj.a

What is x?

Just-glad-it-wasn't-a-bigger-post-ly y'rs

--

Emile van Sebille

---------


Quote:
> Well, how would this work?

> a = ."Ivory the engine"
> ax = obi.a

> What is ax?

> --



Tue, 16 Sep 2003 04:12:09 GMT  
 Quoted identifiers in Python?

Quote:
> 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.

So when I do

x = "turn around"

How does Python know whether I mean the string or the identifier?

--
Remco Gerlich



Tue, 16 Sep 2003 04:15:41 GMT  
 Quoted identifiers in Python?

Quote:
> Well, how would this work?
> a = "Ivory the engine"
> ax = obi.a
> What is ax?

"Ivor(y) the engine" is just an attribute name like any other, so you
couldn't do this any more than you could do:-

obj.name = "Rosy Blade"

x = "name"

y = obj.x

...and expect the y variable to contain "Rosy Blade". There is nothing
special about these quoted identifiers; they are just allowed to contain
characters other than those usually reserved for identifiers.

Quote:

> So when I do

> x = "turn around"

> How does Python know whether I mean the string or the identifier?

We don't necessarily have to use the existing string quote characters to
delimit the quoted identifier. We could use angle brackets <...> or one of

quote character with something else like we do with Unicode strings. So we
could have:-

    x = i"turn around"

or
    x = _"turn around"

or whatever else you prefer. However, we run the risk of the old "static
inline friend void..." problem from C++: we might end up with things like

    iur"turn around"

Anyway, lexical analysis details aside, I think the basic idea of allowing
some form of QIs is a sound one.



Tue, 16 Sep 2003 05:27:20 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. emacs python-mode quote in triple quote font-lock problem

2. identifiers vs. IDENTIFIERS

3. CALL, identifier-2, and address-identifiers

4. Missing identifiers when embedding python in windows

5. replace single quote to double quote with an example

6. quote quote reshape v

7. When is a quote not a quote?

8. single-quoted string conversion to triple-quoted string

9. Quote.py - Look up stock quotes

10. Quote.py - Look up stock quotes

11. Python quote in cgi

12. Python Tutorial: double single quotes?

 

 
Powered by phpBB® Forum Software