Python wish list

Apr 14, 2011 17:34

Now that the moratorium on Python language features is over, I'll put in my thoughts on what new stuff the language could use. I don't have much to suggest, and what I do have to suggest is fairly minor. This is because I'm happy with the language.

new on default parameters

One of the gotchas in python is that default parameters are reused, so if ( Read more... )

Leave a comment

Comments 23

elsmi April 15 2011, 03:15:16 UTC
raw binary conversion -- do you mean struct, or something else?

Reply

bramcohen April 15 2011, 16:31:53 UTC
struct does a reasonable approximation of what I want, but Yech.

Reply


ext_439117 April 15 2011, 03:28:31 UTC
It sounds like the struct module should do what you want for raw binary conversion.

Since the dictionary objects are only kept in the hash table and iteration just walks through that table looking for objects, there'd be no way to scramble the contents in-place. Objects can only be in one place for a given hash value. You'd have to implement this by iterating the dictionary into a list and shuffling that list in place (like random.shuffle does), so you might as well do it yourself.

^ on bytes is an obvious feature, though, you're absolutely right. I might actually write a patch for that myself...

Reply

bramcohen April 15 2011, 05:11:05 UTC
Just changing the hash function which dicts use should scramble things well enough.

Does struct work on arbitrary size integers and allow completely cross-platform operation? The documentation is less than ideal.

Reply

manuzhai April 15 2011, 07:21:45 UTC
The table for the format characters seems clear enough?

Reply

bramcohen April 15 2011, 16:32:52 UTC
Until you've gone through some examples, it's very opaque, and it really, really, would be nice if it clarified whether int sizes might be different on different platforms.

Reply


dictionary scrambling manuzhai April 15 2011, 07:20:42 UTC
Actually it's not the dict that defines the hashing method, it's the __hash__() method of the class of the objects used as keys. So you could probably handle this in your own code by setting the __class__ of the keys to a subclass of their actual __class__, plus a changed __hash__ method?

Reply

Re: dictionary scrambling bramcohen April 15 2011, 16:03:00 UTC
Yeah, I could do something like that, but that would involve extensive ugly changes to my code, when it should just be a single call to the stdlib.

Reply


ciphergoth April 15 2011, 07:35:25 UTC
I don't understand the semantics of your proposed "eggs = new []". If it's "eggs = new list" I understand them.

Reply

bramcohen April 15 2011, 16:02:11 UTC
Saying [] is how you instantiate a new list in Python. It's very non-idiomatic to say list() (although I tried it now and that does work).

Reply

ciphergoth April 15 2011, 16:07:58 UTC
Right, but [] is a list, where as "list" is a function that returns a new list every time you call it. Though I guess "new" could act like "lambda" wrt whatever was on its right.

Reply

bramcohen April 15 2011, 16:35:39 UTC
Yeah, it's sort of acting like a lambda, but you could put an actual lambda there and it would result in the parameter being a function, which is not what you want. Other values should work as well, for example

def spam(eggs = new {3: ['a', 'b', 'c']})

Reply


ciphergoth April 15 2011, 07:38:14 UTC
WRT dictionary scrambling: maybe what you need is a way to arrange for your own class to take the place of "dict"?

Reply

bramcohen April 15 2011, 16:06:44 UTC
Yes, but that would involve extensive changes to my whole codebase for what should be a single call.

Reply

ciphergoth April 15 2011, 16:08:53 UTC
No, I mean a way for Python to use your class whenever you refer to "dict" or {} or similar.

Reply

bramcohen April 15 2011, 16:34:01 UTC
I don't think it has that functionality, not so much monkey patching in Python.

Reply


Leave a comment

Up