-- Beni Cherniavsky <cben@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@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
This isn't strictly about Python, but I went to my daughter's school today, to be a resource person in the computer lab. This one lad (who might be want to learn some Python eventually) showed me how he'd been able to make his borrowed ThinkPad work as an ssh server and a telnet server (he's installed Mandrake 10 on it several times). He'd installed Putty on a Windows 2000 box, and since both the laptop and Windows box had IP numbers from the same school subnet, he was able to ifconfig on Linux, and enter the IP number into putty -- and log into his Linux laptop from Windows, using either ssh or telnet. That pleased him. I just watched, didn't help at all. His parents don't know this stuff. This is a 3rd grader -- 8 years old. Kirby
Jeff Sandys wrote:
-- Beni Cherniavsky <cben@users.sf.net> wrote:
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.
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.
Sure, at run-time a __getattr__ call is just like any other magic method. What I meant that the dot is a bit special *syntactically*. The thing on the left of the dot can be any expression. Its value should be an object with the expected attribute or you would get an exception but syntactically it can be any expression whatsoever. However, syntactically, the thing on the right of the dot is special: it's not an expression at all. As I said, you can't do ``foo.(bar+baz)`` - it's a syntax error. It's an identifier which is taken as a string, not evaluated in the current environment. ``foo.bar`` is not not related to the variable `bar` in the current environment in any way. ``foo+bar`` is. -- Beni Cherniavsky <cben@users.sf.net>, who can only read email on weekends.
participants (3)
-
Beni Cherniavsky
-
Jeff Sandys
-
Kirby Urner