Re: [python-189] Python performance anti-patterns

From: Alex G.
Sent on: Thursday, January 10, 2013 5:08 PM
Please, don't confuse objects and dicts. They're entirely different things. One is an approximately fixed set of attributes combined with behaviors, the other is an arbitrary pairing of keys to values, with no behaviors. These are not the same things.

Alex


On Thu, Jan 10, 2013 at 5:06 PM, Stephen <[address removed]> wrote:
Yeah I've also been wondering if namedtuple is a good standin as a lightweight for writing a class whose
members vary a lot or names or are not statically known at creation time or method-writing time.
But that's not a performance question.

You can make navdata.drone.camera.translation.x resolve to navdata['drone']['camera']['translation']['x']
by rehooking __getattribute__() ; see http://stackoverflow.com/questions/371753/python-using-getattribute-method
Then that hides the implementation details of whether you have a 'proper' object or not and how to access its members.

Stephen


From: [address removed]
To: [address removed]
Subject: Re: [python-189] Python performance anti-patterns
Date: Thu, 10 Jan[masked]:34:18 -0500


How about overuse of dicts as data structures?  Dict ease of use (a really nice thing about Python) can lead to abuse.

There are times when accessing a dict's dict's dict's dict, e.g. navdata['drone']['camera']['translation']['x'] might make sense, but there are times where it feels like a shortcut and I'd like real objects (or at least namedtuples) with the benefits that offers: works with static analysis, better documented/more readable.


On Thu, Jan 10, 2013 at 4:05 PM, Simeon Franklin <[address removed]> wrote:
On Thu, Jan 10, 2013 at 12:34 PM, Alex Gaynor <[address removed]> wrote:
I really cannot imagine anything less import than dict() vs. {}:

I agree with you (and Brendan and Glyph) - I really try to use the language features that make the most sense from a readability perspective. I'm not looking for micro-optimizations here and it sounds like the answer to my question (so far) is a resounding "no".

The example I mentioned (that no longer applies) was worth avoiding. In older versions of Python doing the naively straightforward thing (+= to accumulate a string) could be orders of magnitude slower than appending to an array and then .joining the array on an empty string separator. See eg http://www.skymind.com/~ocrow/python_string/ from circa 2004. All the advice so far is good general advice - profile, use the right data structures, use PyPy - but none of it answers my question directly. In this case a negative result is a good one - I'm happy not to have to mangle my code to avoid "gotchas" in the language*.

-regards
Simeon Franklin

* Actually the one other "gotcha" I've considered is that due to the GIL threads are not as useful as they are in some other languages to deal with performance problems... But that approaches more the "choose the right algorithms and data structures" level of knowledge.




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Simeon Franklin ([address removed]) from San Francisco Python Meetup Group.
To learn more about Simeon Franklin, visit his/her member profile





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by John Wiseman ([address removed]) from San Francisco Python Meetup Group.
To learn more about John Wiseman, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Stephen ([address removed]) from San Francisco Python Meetup Group.
To learn more about Stephen, visit his/her member profile



--
"I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero

Our Sponsors

  • Yelp

    Providing food, beverages, venue, and a good time!

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy