Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: A question on python performance.

Reply
Thread Tools

RE: A question on python performance.

 
 
Joe Goldthwaite
Guest
Posts: n/a
 
      09-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Makes perfect sense to me! Think about it:
>
>method 1: looks up the method directly from the object (fastest)
>method 2: looks up __class__, then looks up __dict__, then gets the
>element from __dict__
>method 3: looks up caller, looks up __class__, looks up __dict__, gets
>element from __dict__
>
>To get the element directly from the object (method 1), Python has to
>internally check __class__.__dict__[element], which shows why method 1
>and method 2 are nearly the same speed. The last version has to look
>up caller in addition to the process described by method 2.
>
>The best way to do what you are doing:
>
>getattr(self, param)(self, *args)


I didn't know about the getattr function. I tried to search for that
type of function but not knowing how to word the search request,
I couldn't find it. That's a lot cleaner way of doing what I was
trying to do. I benchmarked it and it's so close in speed to the
hard-coded method as to be identical.

Paul Hankin wrote:

>You're calling a function (getValue) that just calls a method of trend
>(caller), that just calls another method of trend (Ptd or Qtd or ...).
>You can skip all these steps, and just call the method yourself: the
>code that calls getValue(trend, param, per) replace with
>trend.<something>(per) if you're calling getValue with a static value
>for param, or getattr(trend, param)(per) if param is dynamic.


You're right, it makes sense. Thanks for also pointing out the getattr
function.

Eric Jones wrote:

>What you're describing is a case of mulitple dispatch. See http://

http://www.artima.com/weblogs/viewpo...?thread=101605 for a short
>description and (as short) example by Guido. You can probably fill
>that out and adapt it to your needs. Alternatively, you could look
>into the multimethods module in the Gnosis Utilities package: http://
>pypi.python.org/pypi/Gnosis_Utils/1.2.1-a


Thanks Eric. I read that article on multi-methods but I can't say I
really understand it. I guess I still think of decorators as the people
who got the gym ready for the prom. I've tried getting up to speed on
decorators but I haven't had much success. Like I mentioned previously,
with Python, I still feel like a newbie.

Bruno Desthuilliers wrote:

>IOW, direct access to obj.__class__.__dict__ bypasses both inheritence
>and per-instance overriding.


Thanks for your response also Bruno. I don't understand the code you
posted well enough yet to even ask a useful question. There are a number
of things I haven't seen before. Now I really feel like a newbie!
I'm working on it though so I may have some questions later.

Thanks again for everyone's input. I really appreciate it.






 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-28-2007
Joe Goldthwaite a Úcrit :
(snip)

> I guess I still think of decorators as the people
> who got the gym ready for the prom. I've tried getting up to speed on
> decorators but I haven't had much success.


The @decorator syntax is just syntactic sugar for a common use of higher
order functions. So to understand decorators, you first need understand
the concepts of higher order functions and closures. Both are documented
in many places on the web.

> Bruno Desthuilliers wrote:
>
>> IOW, direct access to obj.__class__.__dict__ bypasses both inheritence
>> and per-instance overriding.

>
> Thanks for your response also Bruno. I don't understand the code you
> posted well enough yet to even ask a useful question. There are a number
> of things I haven't seen before.


It's very 101 python code, apart from this line :
c2.dothis = dothat.__get__(c2, type(c2))

which is a way to dynamically attach a function as method to an
instance, directly invoking the descriptor protocol (it's documented on
python.org)

FWIW, it's most often done using the 'new' module, and possibly less
confusing:

import new
c2.dothis = new.instancemethod(dothat, c2, type(c2))

> Now I really feel like a newbie!


My fault - this example wasn't that necessary, the main point was wrt/
direct access to instance.__class__.__dict__ bypassing normal attribute
lookup rules so inherited attributes are not resolved...

> I'm working on it though so I may have some questions later.


You're welcome.

 
Reply With Quote
 
 
 
 
Magnus Lycka
Guest
Posts: n/a
 
      09-28-2007
Joe Goldthwaite wrote:
> I didn't know about the getattr function. I tried to search for that
> type of function but not knowing how to word the search request,
> I couldn't find it.


You should really read through chapter 2 (Built-in Objects) of the
library reference. All that stuff is core Python functionality that
you should be aware of. Reading chapter 3 (Built-in Types) is also
a really good idea. A lot of the rest is good too, but there is
really no excuse for not knowing the contents of those chapters.
It's just a few pages, and very useful to know. Read them now!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 R. David Murray Python 0 01-17-2011 02:23 PM
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 Senthil Kumaran Python 0 01-17-2011 10:31 AM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Martin v. L÷wis Python 0 03-01-2008 10:51 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Paul Moore Python 0 03-01-2008 10:39 PM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM



Advertisments