Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
On Sat, 14 Jun 2008 04:19:30 pm Cesare Di Mauro wrote: A with statement (such as Pascal's one) can be implemented adding a new scope resolution on top of LEGB. So, taking the FAQ's example, and using a new keyword (because with is already assigned to a different kind of work): def foo(a): on a: print x the block defined by the on statement first must starts looking at the object's namespace. If no symbol was defined inside a, then it follows the traditional LEGB name resolution. I am a great fan of Pascal-style with blocks, and I'd love Python to get something like them. But I have concluded that they simply aren't practical in Python. Your suggestion sounds reasonable at first glance, but it isn't really. def foo(a): on a: x = round(x, n) return x What does this function do? There's no way of telling, because it depends on what attributes a has. It might be equivalent to any of: a.x = a.round(a.x, a.n) # all attributes a.x = round(a.x, a.n) # built-in function round a.x = round(x, a.n) # global x and attribute n etc. and there's no way of predicting which it will be until you know what attributes a has. Even something like this can fail unexpectedly: on a: import math y = math.sin(x) if object a happens to have an attribute called 'math'. Assignament must work on the object's namespace, of course: There's no of course about it. That rule implies that it is impossible to modify the local namespace inside an 'on block', because the object's namespace will always take priority: def foo(a): y = 1 on a: y = x + 1 # creates a new a.y attribute! return y # always returns 1 Perhaps you want this behaviour, but I don't think it is either obvious or desirable. Pascal doesn't have this problem because you always explicitly define your local variables. -- Steven ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
On Sat, Jun 14, 2008 at 10:16 AM, Steven D'Aprano [EMAIL PROTECTED] wrote: I am a great fan of Pascal-style with blocks, and I'd love Python to get something like them. But I have concluded that they simply aren't practical in Python. Your suggestion sounds reasonable at first glance, but it isn't really. def foo(a): on a: x = round(x, n) return x What does this function do? There's no way of telling, because it depends on what attributes a has. It might be equivalent to any of: After having read all the examples in the thread so far, let's never add anything like this to Python ever. It breaks so many of the import this guidelines it's scary. The biggest loss is readability. Sure in a statically typed language it might be possible for the compiler figure out which things are attributes and which are not, but I have to be able to do the same. And lets not even think about what happens when some adds a new attribute to a and code using on a suddenly starts silently breaking. Schiavo Simon ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] multiprocessing source not Unix clean
On Fri, Jun 13, 2008 at 11:31 PM, [EMAIL PROTECTED] wrote: Guido == Guido van Rossum [EMAIL PROTECTED] writes: Guido Ow. definitely. Yes. If I have some time Saturday and nobody beats me to it I'll fix this. Sorry, it seems I've beaten you. :) -- Cheers, Benjamin Peterson There's no place like 127.0.0.1. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
At 08:19 AM 6/14/2008 +0200, Cesare Di Mauro wrote: Assignament must work on the object's namespace, of course: def foo(a): on a: x += 1 print x will be equivalent to: def foo(a): a.x += 1 print a.x Er, you need a syntactic disambiguation here to distinguish attributes from locals or globals: def foo(a): on a: .x += 1 print .x Otherwise, this leads to all sorts of craziness. You'd also have to restrict what could be referenced in a nested on block, in order to avoid further ambiguities. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
In data 14 giugno 2008 alle ore 08:55:58, Martin v. Löwis [EMAIL PROTECTED] ha scritto: This probably belongs to python-ideas or some such, but I don't think this approach can work. People will want to assign to local variables in an ob block, and then be surprised that the assignment actually modified their object: def f(L): total = 0 for h in L: on h: more code accessing h's attributes if x: # reads h.x total = total+1 return total People will be surprised that total is always 0 (and that some objects have an attribute total with a value of 1). Likewise on x: for e in L: counts[e] += 1 # modifies x.counts People will be surprised that x also grows an attribute e, as the for loop involves an assignment, which you say goes to the object's namespace, of course. Regards, Martin I think there can be two solutions to the local variable assignament. The first is to declare them with an local instruction, similar to the global one. So we can have: def f(L): total = 0 for h in L: on h: local total more code accessing h's attributes if x: # reads h.x total = total+1 return total on x: local e for e in L: counts[e] += 1 # modifies x.counts The second solution is to leave assignament to the local namespace, and let assignament to object's attributes happens. So your examples will work without changes, and the object never inherits new attributes. Also, taking the Tk example that I used, it can be changed in the following way: on Button(self) as b: b.text = QUIT b.fg = red b.command = self.quit pack({side: left}) on Button(self) as b: b.text = Hello b.command = self.say_hi pack({side: left}) Using a syntax which reseambles the with one. Cesare Di Mauro ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
Cesare Di Mauro schrieb: Also, taking the Tk example that I used, it can be changed in the following way: on Button(self) as b: b.text = QUIT b.fg = red b.command = self.quit pack({side: left}) on Button(self) as b: b.text = Hello b.command = self.say_hi pack({side: left}) Using a syntax which reseambles the with one. So what is the advantage to b = Button(self) b.text = QUIT b.fg = red b.command = self.quit ? Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] multiprocessing source not Unix clean
On Sat, Jun 14, 2008 at 12:31 AM, [EMAIL PROTECTED] wrote: Guido == Guido van Rossum [EMAIL PROTECTED] writes: Guido On Fri, Jun 13, 2008 at 9:21 AM, [EMAIL PROTECTED] wrote: In trying to solve a build problem with the multiprocessing code on Solaris10 I visited multiprocessing.c in XEmacs and noticed the files all appear to have Windows line endings. Should those maybe be stripped to conform to the other Python source? Guido Ow. definitely. Yes. If I have some time Saturday and nobody beats me to it I'll fix this. FWIW, it appears that Solaris doesn't define SEM_VALUE_MAX but does define _SEM_VALUE_MAX in sys/params.h. .../Modules/_multiprocessing/multiprocessing.c: In function 'init_multiprocessing': .../Modules/_multiprocessing/multiprocessing.c:253: error: 'SEM_VALUE_MAX' undeclared (first use in this function) .../Modules/_multiprocessing/multiprocessing.c:253: error: (Each undeclared identifier is reported only once .../Modules/_multiprocessing/multiprocessing.c:253: error: for each function it appears in.) On Windows the author simple #defines SEM_VALUE_MAX to be LONG_MAX. I used a little cpp action to define it: #ifndef SEM_VALUE_MAX # ifdef _SEM_VALUE_MAX #define SEM_VALUE_MAX _SEM_VALUE_MAX # else #define SEM_VALUE_MAX INT_MAX # endif #endif Guido Does this enable you to submit a patch? Sure, I can submit a patch, though while that got me going I have no real idea if that is the correct way to worm around the problem. I sort of think this is something which should be tested for in configure. The code above was just a guess. Skip I filed http://bugs.python.org/issue3110 for the solaris issue. The best way to fix this is to patch setup.py to in the multiprocessing section to account for the solaris issue. -jesse ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
In data 14 giugno 2008 alle ore 11:03:09, Simon Cross [EMAIL PROTECTED] ha scritto: After having read all the examples in the thread so far, let's never add anything like this to Python ever. It breaks so many of the import this guidelines it's scary. The biggest loss is readability. Sure in a statically typed language it might be possible for the compiler figure out which things are attributes and which are not, but I have to be able to do the same. And lets not even think about what happens when some adds a new attribute to a and code using on a suddenly starts silently breaking. Schiavo Simon It was just a rough idea, and I have suggested two solutions to make it better. Obviously there can be situations where the new instruction will be useful, and other where it will not. In my experience I found so many times coding patterns where I work inside an object, calling many methods and accessing its variabiles (primarily reading them), and the new instruction which I proposed to have in Python can make this kind of code much easier to write and managed (especially with GUIs, there are many common patterns that can benefit from it). Also, I think that the code can be more readable. Just take a look at the example I reported: don't you find it easier to read? Cesare Di Mauro ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
On Sat, Jun 14, 2008 at 5:05 PM, Cesare Di Mauro [EMAIL PROTECTED] wrote: In data 14 giugno 2008 alle ore 21:33:40, Georg Brandl [EMAIL PROTECTED] ha scritto: So what is the advantage to b = Button(self) b.text = QUIT b.fg = red b.command = self.quit ? Georg In this example there are many assignaments, so there aren't many advantages. But this t = ScrolledText.ScrolledText(master, width=60, height=37) t.insert(Tkinter.END, self.log.getText()) t.configure(state=Tkinter.DISABLED) t.see(Tkinter.END) t.pack(fill=Tkinter.BOTH) can look like: on Tkinter: on ScrolledText.ScrolledText(master, width=60, height=37): insert(END, self.log.getText()) configure(state=DISABLED) see(END) pack(fill=BOTH) Then you have to start guessing from where these names came from. Cesare Di Mauro ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/ggpolo%40gmail.com -- -- Guilherme H. Polo Goncalves ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
Just take a look at the example I reported: don't you find it easier to read? No, not at all. I actually *like* having to specify the object on a method call. I find C++/Java code very hard to read which invokes a method without specifying this. infront of the call - in particular when you are unfamiliar with the class hierarchy you are looking at. (due to late binding, it is still difficult to find out what specific method is being called, but atleast you can be certain it's a method, and you might even have a clue what the dynamic type of the object is). So I think the FAQ should continue to claim that Python cannot grow a Pascal-like with statement. Regards, Martin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
In data 14 giugno 2008 alle ore 22:09:28, Guilherme Polo [EMAIL PROTECTED] ha scritto: on Tkinter: on ScrolledText.ScrolledText(master, width=60, height=37): insert(END, self.log.getText()) configure(state=DISABLED) see(END) pack(fill=BOTH) Then you have to start guessing from where these names came from. The same happens with: from Tkinter import * which is a fair common instruction... P.S. Object hierarchy can be a knightmare with or without the new instruction: it's a matter of programmer's knowledge base. Cesare Di Mauro ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
In data 14 giugno 2008 alle ore 22:09:56, Martin v. Löwis [EMAIL PROTECTED] ha scritto: No, not at all. I actually *like* having to specify the object on a method call. I find C++/Java code very hard to read which invokes a method without specifying this. infront of the call - in particular when you are unfamiliar with the class hierarchy you are looking at. (due to late binding, it is still difficult to find out what specific method is being called, but atleast you can be certain it's a method, and you might even have a clue what the dynamic type of the object is). So I think the FAQ should continue to claim that Python cannot grow a Pascal-like with statement. Regards, Martin OK. It was just an idea... Thanks Cesare Di Mauro ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
On Sat, Jun 14, 2008 at 9:53 PM, Cesare Di Mauro [EMAIL PROTECTED] wrote: Just take a look at the example I reported: don't you find it easier to read? Sure, it's perhaps a bit easier on the eyes, but readability includes understanding what's the code does. Let's take an example: on Tkinter: on ScrolledText.ScrolledText(master, width=60, height=37): insert(END, self.log.getText()) configure(state=DISABLED) see(END) pack(fill=BOTH) Is END an attribute of ScrolledText? Tkinter? Neither? Both? Who knows. If we allow assignments it's even more painful. Throwing random junk into the current scope isn't my idea of fun. Schiavo Simon ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
On Sat, Jun 14, 2008 at 10:19 PM, Cesare Di Mauro [EMAIL PROTECTED] wrote: from Tkinter import * I discourage this too. :) Schiavo Simob ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] [Python-3000] First betas (perhaps) on June 18
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Jun 13, 2008, at 9:54 AM, Facundo Batista wrote: 2008/6/13 Barry Warsaw [EMAIL PROTECTED]: My proposal is this: I will spin another release this coming Wednesday, June 18. If we can get both the 2.6 and 3.0 buildbots green by then, and close out all remaining release critical bugs, then Wednesday's release will be beta 1. In that case, I will update PEP 361 and push the entire schedule back by 2 weeks to account for our slippage. Next weekend, 21 and 22, it will be the Python Bug days. Maybe it's worth to delay the betas one week to include this work? I think I'd rather stick to a one week delay, otherwise I think we're really talking about pushing the whole schedule back a month. I think we should focus right now on getting the buildbots as green as possible (yay for some 3.0 greenness! thanks everyone!) and closing all the release critical bugs. I think this will actually free us up a bit more on bug day to concentrate on fixing other issues. Cheers, - -Barry -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (Darwin) iQCVAwUBSFQ1qnEjvBPtnXfVAQI13wP/Xj+my8YUNHuLG8i7pggHNC1Vn7/j5K7H 4l5vT9EMgkT6OHXKvrVPxOJSbm7TkoHd4k3M524IYwnKigFAVH/e9WmgTChp4hPv 7UsF9MGKcSsgnjB2LpWvbV9A6lSRLsNLjuLqqJSQgS7TvrD+0Omd91RM9twmV9io BndCSNAALyE= =g3RH -END PGP SIGNATURE- ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] [Python-3000] First betas (perhaps) on June 18
On Sat, Jun 14, 2008 at 4:18 PM, Barry Warsaw [EMAIL PROTECTED] wrote: On Jun 13, 2008, at 9:54 AM, Facundo Batista wrote: Next weekend, 21 and 22, it will be the Python Bug days. Maybe it's worth to delay the betas one week to include this work? I think I'd rather stick to a one week delay, otherwise I think we're really talking about pushing the whole schedule back a month. I think we should focus right now on getting the buildbots as green as possible (yay for some 3.0 greenness! thanks everyone!) and closing all the release critical bugs. I think this will actually free us up a bit more on bug day to concentrate on fixing other issues. I kinda hate to do this to you, but I had to file another release blocker: #3114 -- Cheers, Benjamin Peterson There's no place like 127.0.0.1. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
On Sat, Jun 14, 2008 at 1:24 PM, Simon Cross [EMAIL PROTECTED] wrote: On Sat, Jun 14, 2008 at 10:19 PM, Cesare Di Mauro [EMAIL PROTECTED] wrote: from Tkinter import * I discourage this too. :) And so do I, every single time I teach Python (which is pretty often, even though many of those cases don't make it into YouTube videos). I don't need to rant against it within Google, because Google's Python Style Guide already absolutely forbids it, fortunately;-). Alex ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
Armin Ronacher wrote: Hi, I noticed lately that quite a few projects are implementing their own subclasses of `dict` that retain the order of the key/value pairs. However half of the implementations I came across are not implementing the whole dict interface which leads to weird bugs, also the performance of a Python implementation is not that great. I'm +1 - but this proposal has been made many times before and people always argue about what features are needed or desirable. :-( Michael Foord To fight that problem I want to proposed a new class in collections called odict which is a dict that keeps the items sorted, similar to a PHP array. The interface would be fully compatible with dict and implemented as dict subclass. Updates to existing keys does not change the order of a key but new keys are inserted at the end. Additionally it would support slicing where a list of key, value tuples is returned and sort/reverse/index methods that work like their list equivalents. Index based lookup could work via odict.byindex(). An implementation of that exists as part of the ordereddict implementation which however goes beyond that and is pretty much a fork of the python dict[1]. Some reasons why ordered dicts are a useful feature: - in XML/HTML processing it's often desired to keep the attributes of an tag ordered during processing. So that input ordering is the same as the output ordering. - Form data transmitted via HTTP is usually ordered by the position of the input/textarea/select field in the HTML document. That information is currently lost in most Python web applications / frameworks. - Eaiser transition of code from Ruby/PHP which have sorted associative arrays / hashmaps. - Having an ordered dict in the standard library would allow other libraries support them. For example a PHP serializer could return odicts rather then dicts which drops the ordering information. XML libraries such as etree could add support for it when creating elements or return attribute dicts. Regards, Armin [1]: http://www.xs4all.nl/~anthon/Python/ordereddict/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk -- http://www.ironpythoninaction.com/ http://www.theotherdelia.co.uk/ http://www.voidspace.org.uk/ http://www.ironpython.info/ http://www.resolverhacks.net/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
Cesare Di Mauro wrote: However, I don't agree with the FAQ on this point. I think that a Pascal-like with statement can be achieved, even with a dynamic language such as Python, and in a simple way. It's not so much a matter of whether it *can* be done, but whether there's any substantial need for it. I don't believe there is in Python, because you can always put a reference to the target object into a succinctly-named local and access it through that. You can't always do that in Pascal, because it provides no way of obtaining a pointer a record that wasn't heap-allocated. -- Greg ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
* Armin Ronacher wrote: Some reasons why ordered dicts are a useful feature: - in XML/HTML processing it's often desired to keep the attributes of an tag ordered during processing. So that input ordering is the same as the output ordering. - Form data transmitted via HTTP is usually ordered by the position of the input/textarea/select field in the HTML document. That information is currently lost in most Python web applications / frameworks. - Eaiser transition of code from Ruby/PHP which have sorted associative arrays / hashmaps. - Having an ordered dict in the standard library would allow other libraries support them. For example a PHP serializer could return odicts rather then dicts which drops the ordering information. XML libraries such as etree could add support for it when creating elements or return attribute dicts. I find this collection of cases pretty weak as an argument for implementing that in the stdlib. A lot of special purpose types would fit into such reasoning, but do you want to have all of them maintained here? nd -- Da fällt mir ein, wieso gibt es eigentlich in Unicode kein i mit einem Herzchen als Tüpfelchen? Das wär sooo süüss! -- Björn Höhrmann in darw ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
Hi, Armin Ronacher armin.ronacher at active-4.com writes: To fight that problem I want to proposed a new class in collections called odict which is a dict that keeps the items sorted, similar to a PHP array. I'm also +1 on this. I've used the implementation you mentioned in a compiler project of mine and found it to be quite useful. It is hard for me to mention any other uses but there definitely are many occasions where people need them and either go for (key, value)-tuples or use some lib which does only provide this single data type. I am pretty optimistic that the addition will find its usecases, once it is in the stdlib. regards, Marek ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
Cesare Di Mauro wrote: The same happens with: from Tkinter import * which is a fair common instruction... ...and which should *not* be used in most cases, for the same reason. All those tutorials that start out with 'from something import *' are doing a lot of harm to the impressionable minds of new programmers, IMO. -- Greg ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Advice on numbers.py implementation of binary mixins.
Raymond Hettinger wrote: PEP-3141 outlines an approach to writing binary operators to allow the right operand to override the operation if the left operand inherits the operation from the ABC. Here is my first approximation at how to write them for the Integral mixins: class Integral(Rational): def __and__(self, other): if isinstance(other, (type(self), int, long)): # XXX return int(self) int(other) I think for this mixin, it doesn't matter whether other is an Integral instance; it matter whether it is has a more specific solution. So instead of checking whether isinstance, check whether its __rand__ method is Integral.__rand__. I think you also may want to guard against incomplete right-hand operations, by doing something like replacing the simple return NotImplemented with try: val = other.__rand__(self) if val is not NotImplemented: return val except (TypeError, AttributeError): pass # Use the generic fallback after all return int(self) int(other) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] Proposal: add odict to collections
The odict (as proposed here, ordered on time of key insertion) looks like a close match to the dlict needed by some of the optimization proposals. http://python.org/dev/peps/pep-0267/ -jJ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
On Sat, Jun 14, 2008 at 4:57 PM, André Malo [EMAIL PROTECTED] wrote: * Armin Ronacher wrote: Some reasons why ordered dicts are a useful feature: - in XML/HTML processing it's often desired to keep the attributes of an tag ordered during processing. So that input ordering is the same as the output ordering. - Form data transmitted via HTTP is usually ordered by the position of the input/textarea/select field in the HTML document. That information is currently lost in most Python web applications / frameworks. - Eaiser transition of code from Ruby/PHP which have sorted associative arrays / hashmaps. - Having an ordered dict in the standard library would allow other libraries support them. For example a PHP serializer could return odicts rather then dicts which drops the ordering information. XML libraries such as etree could add support for it when creating elements or return attribute dicts. I find this collection of cases pretty weak as an argument for implementing that in the stdlib. A lot of special purpose types would fit into such reasoning, but do you want to have all of them maintained here? No, but an ordered dict happens to be a *very* common thing to need, for a variety of reasons. So I'm +0.5 on adding this to the collections module. However someone needs to contribute working code. It would also be useful to verify that it actually fulfills the needs of some actual use case. Perhaps looking at how Django uses its version would be helpful. -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
Michael Foord wrote: Armin Ronacher wrote: Hi, I noticed lately that quite a few projects are implementing their own subclasses of `dict` that retain the order of the key/value pairs. However half of the implementations I came across are not implementing the whole dict interface which leads to weird bugs, also the performance of a Python implementation is not that great. I'm +1 - but this proposal has been made many times before and people always argue about what features are needed or desirable. :-( There's been a lot of controversy/confusion about ordered dicts. One of the sources of confusion is that people mean different things when they use the term ordered dict: In some cases, the term is used to mean a dictionary that remembers the order of insertions, and in other cases it is used to mean a sorted dict, i.e. an associative data structure in which the entries are kept sorted. (And I'm not sure that those are the only two possibilities.) I would be more in favor of the idea if we could come up with a less ambiguous naming scheme. -- Talin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
On Sat, Jun 14, 2008 at 11:39 PM, Armin Ronacher [EMAIL PROTECTED] wrote: Some reasons why ordered dicts are a useful feature: And for metaclasses or for LRU caches or for removing duplicates in a list while maintaining order. I think that the name ordereddict would be more readable though, and match the existing defaultdict class in the collections module. -- mvh Björn ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
2008/6/14 Talin [EMAIL PROTECTED]: There's been a lot of controversy/confusion about ordered dicts. One of the sources of confusion is that people mean different things when they use the term ordered dict: In some cases, the term is used to mean a dictionary that remembers the order of insertions, and in other cases it is used to mean a sorted dict, i.e. an associative data structure in which the entries are kept sorted. (And I'm not sure that those are the only two possibilities.) Have the comparison function passed in as a parameter then, if it's None, then have it maintain the order of insertion? Something like: def __init__(self, cmpfunc = None): self.dict = dict() def __getattr__(self, key): try: return self.key -- Cheers, Hasan Diwan [EMAIL PROTECTED] ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python FAQ: Why doesn't Python have a with statement?
Cesare Di Mauro wrote: I agree: Python's with statement does have a completely different meaning of the same Pascal Co 's statement. However, I don't agree with the FAQ on this point. I think that a Pascal-like with statement can be achieved, even with a dynamic language such as Python, and in a simple way. It isn't that a Pascal-with-style statement can't be achieved, it's that it is pointless syntactic sugar in Python. Just use o = long_complicated_object_name instead. Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --- http://www.boredomandlaziness.org ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
Talin wrote: Michael Foord wrote: Armin Ronacher wrote: Hi, I noticed lately that quite a few projects are implementing their own subclasses of `dict` that retain the order of the key/value pairs. However half of the implementations I came across are not implementing the whole dict interface which leads to weird bugs, also the performance of a Python implementation is not that great. I'm +1 - but this proposal has been made many times before and people always argue about what features are needed or desirable. :-( There's been a lot of controversy/confusion about ordered dicts. One of the sources of confusion is that people mean different things when they use the term ordered dict: In some cases, the term is used to mean a dictionary that remembers the order of insertions, and in other cases it is used to mean a sorted dict, i.e. an associative data structure in which the entries are kept sorted. (And I'm not sure that those are the only two possibilities.) I would be more in favor of the idea if we could come up with a less ambiguous naming scheme. I think Armin's proposal addresses this nicely by the analogy to lists: the ordered dict is in key insertion order by default, but you can invoke odict.sort() to sort it instead. Given the synergy with the Py3k metaclass enhancements, I believe this would be a good thing to have. Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --- http://www.boredomandlaziness.org ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] PEP 8 and optional underscores
Benjamin Peterson wrote: On Fri, Jun 13, 2008 at 12:14 PM, Guido van Rossum [EMAIL PROTECTED] wrote: On Fri, Jun 13, 2008 at 9:42 AM, Benjamin Peterson [EMAIL PROTECTED] wrote: On Fri, Jun 13, 2008 at 11:40 AM, Raymond Hettinger [EMAIL PROTECTED] wrote: Nick def getName(self): Nick assert self.__initialized, Thread.__init__() not called Nick return self.__name Why is __name private to begin with? Any reason for the getters and setters? Why isn't this just an attribute? In 3.x, it's just an attribute. Oh, is it? Where's the PEP with the API redesign? Did I miss some kind of decision-making process, weighing compatibility concerns against other issues? meaning that it only has one underscore. They methods still live. That's worse - it means it can now collide with _name attributes on subclasses. This has become a lot more complicated than what I thought we were doing: adding PEP 8 compliant aliases for the existing methods without otherwise changing the threading implementation. As far as I can recall, that is all that was contained in the 2.x patch I reviewed. Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --- http://www.boredomandlaziness.org ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
Guido van Rossum guido at python.org writes: On Sat, Jun 14, 2008 at 4:57 PM, André Malo nd at perlig.de wrote: I find this collection of cases pretty weak as an argument for implementing that in the stdlib. A lot of special purpose types would fit into such reasoning, but do you want to have all of them maintained here? No, but an ordered dict happens to be a *very* common thing to need, for a variety of reasons. So I'm +0.5 on adding this to the collections module. However someone needs to contribute working code. It would also be useful to verify that it actually fulfills the needs of some actual use case. Perhaps looking at how Django uses its version would be helpful. I compared multiple ordered dicts now (including Babel, Django and the C-implementation I mentioned earlier) and implemented a python version of the ordered dict as reference implementation: http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py Regards, Armin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
BJörn Lindqvist bjourne at gmail.com writes: I think that the name ordereddict would be more readable though, and match the existing defaultdict class in the collections module. While I agree that ordered makes more sense, it's pretty stupid to type with two 'd's right after another. Regards, Armin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
From: Talin [EMAIL PROTECTED] There's been a lot of controversy/confusion about ordered dicts. I think that is why all earlier proposals all died. One of the sources of confusion is that people mean different things when they use the term ordered dict: In some cases, the term is used to mean a dictionary that remembers the order of insertions, and in other cases it is used to mean a sorted dict, i.e. an associative data structure in which the entries are kept sorted. (And I'm not sure that those are the only two possibilities.) For an insertion order dictionary, there was also a question about how to handle duplicate keys. Given odict([(k1,v1), (k2,v2), (k1,v3)]), what should the odict.items() return? [(k1,v3), (k2,v2)] [(k2,v2), (k1,v3)] The first maintains the original sort order and is consistent with the usual idea that d[k]=v simply replaces the value but does not alter the hash table. It is a bit weird though because v3 appears earlier than v2 which was inserted earlier. It's especially weird for keys that are equal but not identical: d[0]=v1 d[0.0]=v3. Do you want 0.0 to remain associated with v3 or should the items list contain a pair that was not in the original insertion list, (0, v3)? The second one is a bit weird because a key loses its place whenever the value is updated. IIRC, previous discussions showed an interest in odicts but that there were a lot of questions of the specific semantics of the API. This would no doubt be compounded by attempts to emulate dict views. Regardless, there should probably be a PEP and alternate pure python versions should be posted on ASPN so people can try them out. post Raymond ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
Hasan Diwan hasan.diwan at gmail.com writes: 2008/6/14 Talin talin at acm.org: There's been a lot of controversy/confusion about ordered dicts. One of the sources of confusion is that people mean different things when they use the term ordered dict: In some cases, the term is used to mean a dictionary that remembers the order of insertions, and in other cases it is used to mean a sorted dict, i.e. an associative data structure in which the entries are kept sorted. (And I'm not sure that those are the only two possibilities.) Have the comparison function passed in as a parameter then, if it's None, then have it maintain the order of insertion? Something like: def __init__(self, cmpfunc = None): self.dict = dict() I think that would be contraproductive and would make the constructor incompatible with the normal dict constructor which accepts keyword arguments too. Also that dict is just in order, but not sorted by something. You could still do something like this: d = odict() d['Pinguin'] = 'telly' d['Parrot'] = 'cage' d['Mouse'] = 'Napoleon' d.sort(key=lambda x: x[1].lower()) That of course would not sort items inserted after the sort call. Regards, Armin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Proposal: add odict to collections
On 14-Jun-08, at 8:39 PM, Armin Ronacher wrote: ... I noticed lately that quite a few projects are implementing their own subclasses of `dict` that retain the order of the key/value pairs. ... I'm +1 on this one too, as there are at least a couple of times in recent memory when I would have found this useful. And, as far as questions about the definition of an ordered dictionary, is there any good reason not to simply treat the dict as a list? Something like (with the obvious bits left out): class odict(dict): def __init__(self, *args): self._order = [] def __setitem__(self, key, val): if key not in self: self._order.append(key) def __iter__(self): return self._order def items(self): return ([item, self[item] for item in self._order]) def sort(self): self._order.sort() ... and so on ... That way all the order-related functions are well defined, so it would be hard claim that it doesn't do the right thing without claiming that lists don't do the right thing. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com