[Edu-sig] Re: A better way?
Jeff Sandys
sandysj at juno.com
Sat Dec 4 02:46:38 CET 2004
-- Beni Cherniavsky <cben at users.sf.net> wrote:
Jeff Sandys wrote:
> I showed this thread at the SeaPIG meeting and we
> talked about handling attributes. I pointed out
> that the dot is an operator and that you can have
> a space between the dot, and the group didn't
> believe me until they tried it.
>
>>>>s = "my string"
>>>>s . split()
>
> ['my', 'string']
>
> I think by showing the students that dot is an
> operator helps them understand what is going on
> and they might not be so concerned about making
> the long variable and method dotted chains.
>
It's important that they realize that the dot is not a
pure operator as the rest of Pythons operators: the thing
on its right side is not an arbitrary expression but an
identifier. So you can't do ``foo . (bar + baz)`` the way
you can do ``foo * (bar + baz)``. Showing the equivallence
to `getattr()` is a nice way to demonstarte the fact the
right argument is not an expression evaluated in the current environment but actually just a string.
--
Beni Cherniavsky <cben at users.sf.net>, who can only read email on weekends.
There you go making dot (.) a special operator. ;->
It is no more special than plus (+), star (*), or
percent (%). As you said objects need to have a __getattr__
method for dot to work just as they need __add__ method for
plus to work, __mul__ method for star to work and __mod__
method for percent to work. Both numbers and strings have
all three methods. The thing on the left or right is never
an arbitrary expression, it is an object that might have
the corresponding method for the given operator.
>>> a = 5
>>> a . __mod__(2)
1
>>> b = "aaa %s ccc"
>>> b . __mod__("bbb")
'aaa bbb ccc'
I saw where this company, LiveLogix, has an extention of
Python that added a "defop" function, that is kind of
like a macro pattern language, that allows you to define
infix operations.
http://logix.livelogix.com/intro.html
Thanks,
Jeff Sandys
________________________________________________________________
Juno Gift Certificates
Give the gift of Internet access this holiday season.
http://www.juno.com/give
More information about the Edu-sig
mailing list