Question about email-handling modules

thebjorn BjornSteinarFjeldPettersen at gmail.com
Fri Dec 21 05:31:31 EST 2007


On Dec 20, 4:15 pm, Robert Latest <boblat... at yahoo.com> wrote:
> Steven D'Aprano wrote:
[...]
> > All methods are attributes (although the opposite is not the case), so if
> > a method doesn't exist, you will get an AttributeError.
>
> I see. I've already gathered that Python likes to use different words for
> common things (attribute instead of member or method).

..we were hoping it would make you feel comfy when coming from
Perl ;-)

On a more serious note, Python is actually quite semantically regular:
a dot (.) always means the same thing, as do parens. It might not be
immediately obvious exactly _what_ it means if you're coming from
languages that confuse issues with syntactic sweetness.

When you see code that says

    foo.bar(baz)

there are two distinct operations happening, namely

    tmp = foo.bar      # attribute lookup (the dot-operator)
    tmp(baz)           # function call (the paren-operator)

this will give you the insight to one of the first optimization
methods you can use if a loop is a bottleneck

    for i in range(100000):
        foo.bar(i)           # profiling says this is a bottleneck

attribute lookup hoisting optimization

    tmp = foo.bar            # move attribute lookup outside the loop
    for i in range(100000):
        tmp(i)

in the interest of full disclosure, I should probably mention that I'm
of course lying to you ;-)  You can override both attribute lookup and
function call in your own classes, but (a) that shouldn't be important
to you at this point *wink*, and (b) at that level Python is quite
semantically regular.

-- bjorn



More information about the Python-list mailing list