Re: Pythonic style

2020-09-23 Thread Stavros Macrakis
When doesn't it apply? Can you elaborate on this? It might be easier > to advise on Pythonic style when the specific requirements are known. > No specific requirement. These are *finger exercises* intended to isolate one issue for discussion rather than be useful in themselves. >

Re: Pythonic style

2020-09-22 Thread Chris Angelico
27;t *use > tuple unpacking, on the theory that the coding patterns may be useful in > other cases where unpacking doesn't apply. When doesn't it apply? Can you elaborate on this? It might be easier to advise on Pythonic style when the specific requirements are known. > For me, one of th

Re: Pythonic style

2020-09-22 Thread Stavros Macrakis
Thanks to everyone for the comments, especially Tim Chase for the simple and elegant tuple unpacking solution, and Léo El Amri for the detailed comments on the variants. Below are some more variants which *don't *use tuple unpacking, on the theory that the coding patterns may be useful in other cas

Re: Pythonic style

2020-09-21 Thread Stavros Macrakis
Thanks, Tim! I didn't realize that you could write (x,) on the LHS! Very nice, very Pythonic! -s On Mon, Sep 21, 2020 at 9:15 AM Tim Chase wrote: > On 2020-09-20 18:34, Stavros Macrakis wrote: > > Consider a simple function which returns the first element of an > > iterable if it

Re: Pythonic style

2020-09-21 Thread Terry Reedy
On 9/20/2020 6:34 PM, Stavros Macrakis wrote: I'm trying to improve my Python style. Consider a simple function which returns the first element of an iterable if it has exactly one element, and throws an exception otherwise. It should work even if the iterable doesn't terminate. I've written thi

Re: Pythonic style

2020-09-21 Thread Frank Millman
On 2020-09-21 3:46 PM, Chris Angelico wrote: On Mon, Sep 21, 2020 at 11:37 PM Tim Chase wrote: On 2020-09-20 18:34, Stavros Macrakis wrote: Consider a simple function which returns the first element of an iterable if it has exactly one element, and throws an exception otherwise. It should wor

Re: Pythonic style

2020-09-21 Thread Tim Chase
On 2020-09-21 09:48, Stavros Macrakis wrote: >> def fn(iterable): >> x, = iterable >> return x > > Thanks, Tim! I didn't realize that you could write (x,) on the LHS! > Very nice, very Pythonic! It also expands nicely for other cases, so you want the 3-and-only-3 first values with errors

Re: Pythonic style

2020-09-21 Thread Léo El Amri via Python-list
On 21/09/2020 15:15, Tim Chase wrote: > You can use tuple unpacking assignment and Python will take care of > the rest for you: > > so you can do > > def fn(iterable): > x, = iterable > return x > > I'm not sure it qualifies as Pythonic, but it uses Pythonic features > like tuple unpac

Re: Pythonic style

2020-09-21 Thread Chris Angelico
On Mon, Sep 21, 2020 at 11:37 PM Tim Chase wrote: > > On 2020-09-20 18:34, Stavros Macrakis wrote: > > Consider a simple function which returns the first element of an > > iterable if it has exactly one element, and throws an exception > > otherwise. It should work even if the iterable doesn't ter

Re: Pythonic style

2020-09-21 Thread Tim Chase
On 2020-09-20 18:34, Stavros Macrakis wrote: > Consider a simple function which returns the first element of an > iterable if it has exactly one element, and throws an exception > otherwise. It should work even if the iterable doesn't terminate. > I've written this function in multiple ways, all of

Re: Pythonic style

2020-09-21 Thread Léo El Amri via Python-list
On 21/09/2020 00:34, Stavros Macrakis wrote: > I'm trying to improve my Python style. > > Consider a simple function which returns the first element of an iterable > if it has exactly one element, and throws an exception otherwise. It should > work even if the iterable doesn't terminate. I've writ

Pythonic style

2020-09-21 Thread Stavros Macrakis
I'm trying to improve my Python style. Consider a simple function which returns the first element of an iterable if it has exactly one element, and throws an exception otherwise. It should work even if the iterable doesn't terminate. I've written this function in multiple ways, all of which feel a

Re: Pythonic style

2016-04-29 Thread Steven D'Aprano
On Fri, 29 Apr 2016 10:48 am, Gregory Ewing wrote: > MRAB wrote: > >> Is it worthy of being in the Zen of Python? > > +1. Maybe something along the lines of: > > Dunder methods are for defining, not calling. > Unless you're a dunderhead[1]. > > [1] Meant in the sense of an enthusiast,

Re: Pythonic style

2016-04-28 Thread Chris Angelico
On Fri, Apr 29, 2016 at 4:12 PM, Gregory Ewing wrote: > Chris Angelico wrote: >> >> I thought the twentieth zen would never be found? > > > Yes. This will have to be numbered the 21st zen > to maintain that invariant. > Python for the 21st Century. In a hundred years, another zen! ChrisA -- ht

Re: Pythonic style

2016-04-28 Thread Gregory Ewing
Chris Angelico wrote: I thought the twentieth zen would never be found? Yes. This will have to be numbered the 21st zen to maintain that invariant. -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: Pythonic style

2016-04-28 Thread Chris Angelico
On Fri, Apr 29, 2016 at 10:48 AM, Gregory Ewing wrote: > MRAB wrote: > >> Is it worthy of being in the Zen of Python? > > > +1. Maybe something along the lines of: > >Dunder methods are for defining, not calling. >Unless you're a dunderhead[1]. > > [1] Meant in the sense of an enthusiast,

Re: Pythonic style

2016-04-28 Thread Gregory Ewing
MRAB wrote: Is it worthy of being in the Zen of Python? +1. Maybe something along the lines of: Dunder methods are for defining, not calling. Unless you're a dunderhead[1]. [1] Meant in the sense of an enthusiast, cf. gearhead. -- Greg -- https://mail.python.org/mailman/listinfo/pytho

Re: Pythonic style

2016-04-28 Thread Random832
On Thu, Apr 28, 2016, at 01:16, Rustom Mody wrote: > On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote: > > My rule of thumb is: Dunders are for defining, not for calling. It's > > not a hard-and-fast rule, but it'll get you through 99%+ of > > situations. > > Neat and cleve

Re: Dunder docs again (was Pythonic style)

2016-04-28 Thread MRAB
On 2016-04-28 13:25, Marko Rauhamaa wrote: MRAB : 'pythonic-ness'? Surely it's 'pythonicity'! :-) Doubt it: Full Definition of generic [...] —generically \-i-k(ə-)lē\ adverb —genericness noun http://www.merriam-webster.com/dictionary/generic> I can point you to 3 alternatives,

Re: Dunder docs again (was Pythonic style)

2016-04-28 Thread Marko Rauhamaa
MRAB : > 'pythonic-ness'? Surely it's 'pythonicity'! :-) Doubt it: Full Definition of generic [...] —generically \-i-k(ə-)lē\ adverb —genericness noun http://www.merriam-webster.com/dictionary/generic> Marko -- https://mail.python.org/mailman/listinfo/python-list

Re: Dunder docs again (was Pythonic style)

2016-04-28 Thread MRAB
On 2016-04-28 12:45, Rustom Mody wrote: On Thursday, April 28, 2016 at 4:46:43 PM UTC+5:30, MRAB wrote: On 2016-04-28 06:16, Rustom Mody wrote: > On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote: >> My rule of thumb is: Dunders are for defining, not for calling. It's >> n

Dunder docs again (was Pythonic style)

2016-04-28 Thread Rustom Mody
On Thursday, April 28, 2016 at 4:46:43 PM UTC+5:30, MRAB wrote: > On 2016-04-28 06:16, Rustom Mody wrote: > > On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote: > >> My rule of thumb is: Dunders are for defining, not for calling. It's > >> not a hard-and-fast rule, but it'll

Re: Pythonic style

2016-04-28 Thread MRAB
On 2016-04-28 06:16, Rustom Mody wrote: On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote: My rule of thumb is: Dunders are for defining, not for calling. It's not a hard-and-fast rule, but it'll get you through 99%+ of situations. Neat and clever. Should get in the docs

Re: Pythonic style

2016-04-27 Thread Steven D'Aprano
On Thursday 28 April 2016 13:23, Ben Finney wrote: > Christopher Reimer writes: > >> In short, my original code before I turned it into a separate >> dictionary. *sigh* > > No, I think that misses the points that were being made. The discussion > you're talking about was *not* to say “attribut

Re: Pythonic style

2016-04-27 Thread Rustom Mody
On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote: > My rule of thumb is: Dunders are for defining, not for calling. It's > not a hard-and-fast rule, but it'll get you through 99%+ of > situations. Neat and clever. Should get in the docs somewhere -- https://mail.python.org

Re: Pythonic style

2016-04-27 Thread Christopher Reimer
On 4/27/2016 8:52 PM, Ethan Furman wrote: The point Ben was trying to make is this: you should never* call __dunder__ methods in normal code; there is no need to do so: - use len(), not __len__() - use next(), not __next__() - use some_instance.an_attribute, not some_instance.__dict__['an_a

Re: Pythonic style

2016-04-27 Thread Christopher Reimer
On 4/27/2016 8:23 PM, Ben Finney wrote: If you want items in a mapping, explicitly use a Python ‘dict’ instance. If you want attributes that describe an object, explicitly use attributes of that object. Deliberately choose which one makes more sense. Okay, that makes sense. Thank you, Chris R

Re: Pythonic style

2016-04-27 Thread Chris Angelico
On Thu, Apr 28, 2016 at 1:52 PM, Ethan Furman wrote: > > The point Ben was trying to make is this: you should never* call __dunder__ > methods in normal code; there is no need to do so: > > - use len(), not __len__() > - use next(), not __next__() > - use some_instance.an_attribute, not some_inst

Re: Pythonic style

2016-04-27 Thread Ethan Furman
On 04/27/2016 08:07 PM, Christopher Reimer wrote: On 4/27/2016 7:07 PM, Ben Finney wrote: >> Ian Kelly wrote: self.__dict__ = {'key', 'value'} is essentially equivalent to: self.key = value >> I would say the latter is more Pythonic, because it: >> >> [snip] >> * Uses the built

Re: Pythonic style

2016-04-27 Thread Ben Finney
Christopher Reimer writes: > In short, my original code before I turned it into a separate > dictionary. *sigh* No, I think that misses the points that were being made. The discussion you're talking about was *not* to say “attribute access is better than dictionary access”, or vice versa. Each

Re: Pythonic style (was: Differences between Class(Object) and Class(Dict) for dictionary usage?)

2016-04-27 Thread Christopher Reimer
On 4/27/2016 7:07 PM, Ben Finney wrote: I would say the latter is more Pythonic, because it: * Better conveys the intention (“set the value of the ‘self.key’ attribute”). * Uses the built-in mechanisms of Python (don't invoke magic attributes, instead use the system that makes use of them

Pythonic style (was: Differences between Class(Object) and Class(Dict) for dictionary usage?)

2016-04-27 Thread Ben Finney
Christopher Reimer writes: > On 4/27/2016 7:33 AM, Ian Kelly wrote: > > > self.__dict__ = {'key', 'value'} > > > > self.key = value > > Which expression is Pythonic? (Note that assignment is not an expression in Python; assigment is a statement.) > I've seen both used in various examp

Re: rot13 in a more Pythonic style?

2007-02-16 Thread Andy Dingley
On 14 Feb, 20:06, "Beej" <[EMAIL PROTECTED]> wrote: > http://linuxgazette.net/109/pramode.html Thanks, that's a _really_ interesting link (Oh, I need to learn Scheme!) My code now looks like this, which I'm starting to feel much happier about in a functional sense. c_rot = lambda c, chars :

Re: rot13 in a more Pythonic style?

2007-02-16 Thread Steven D'Aprano
On Fri, 16 Feb 2007 04:53:23 +, Dennis Lee Bieber wrote: > On 15 Feb 2007 11:10:53 -0800, "Andy Dingley" <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > >> >> Fortunately I don't think it's _quite_ that bad. > > Possibly not, but that description of the problem wo

Re: rot13 in a more Pythonic style?

2007-02-15 Thread Andy Dingley
On 15 Feb, 17:55, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > Sounds more like a case for a parser/lexer wherein the emitted "code > tokens" are the "new style" identifiers... 8-(I'm trying not to think about that Fortunately I don't think it's _quite_ that bad. -- http://ma

Re: rot13 in a more Pythonic style?

2007-02-15 Thread [EMAIL PROTECTED]
On Feb 14, 11:46 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Wed, 14 Feb 2007 14:04:17 -0300, Andy Dingley <[EMAIL PROTECTED]> > escribió: > > > I still don't understand what a lambda is _for_ in Python. I know what > > they are, I know what the alternatives are, but I still haven't fou

Re: rot13 in a more Pythonic style?

2007-02-15 Thread Andy Dingley
On 14 Feb, 21:59, Paul Rubin wrote: > Why don't you describe the actual problem instead of the rot13 analogy. I don't know what the actual problem is! I need to perform a complex mapping between "old style" structured identifiers and "new style" structured identifers. A

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Martin P. Hellwig
[EMAIL PROTECTED] wrote: > Martin P. Hellwig >> for me (personal) being Pythonic means that I should >> separate the logic and variables, etc... > > Well, for me me Pythonic means using built-in functionalities as much > as possible (like using encode("rot13") or translate), and to write > less co

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Paul Rubin
"Andy Dingley" <[EMAIL PROTECTED]> writes: > c_rot13 = lambdaf c : (((c, uc_rot13(c)) [c in > 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']), lc_rot13(c) )[c in > 'abcdefghijklmnopqrstuvwxyz'] Oh, I see what you mean, you have separate upper and lower case maps and you're asking how to select one in an expression

Re: rot13 in a more Pythonic style?

2007-02-14 Thread bearophileHUGS
Martin P. Hellwig > for me (personal) being Pythonic means that I should > separate the logic and variables, etc... Well, for me me Pythonic means using built-in functionalities as much as possible (like using encode("rot13") or translate), and to write less code, (avoiding overgeneralizations fro

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Paul Rubin
"Andy Dingley" <[EMAIL PROTECTED]> writes: > I'm trying to write rot13, but to do it in a better and more Pythonic > style than I'm currrently using. What would you reckon to the > following pretty ugly thing? How would you improve it? In > partic

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Rob Wolfe
"Andy Dingley" <[EMAIL PROTECTED]> writes: > I'm trying to write rot13, but to do it in a better and more Pythonic > style than I'm currrently using. What would you reckon to the > following pretty ugly thing? How would you improve it? In > partic

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Beej
On Feb 14, 9:04 am, "Andy Dingley" <[EMAIL PROTECTED]> wrote: > I still don't understand what a lambda is _for_ in Python. Python supports functional programming to a certain extent, and lambdas are part of this. http://linuxgazette.net/109/pramode.html > I know what > they are, I know what the

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Gabriel Genellina
En Wed, 14 Feb 2007 14:04:17 -0300, Andy Dingley <[EMAIL PROTECTED]> escribió: > I still don't understand what a lambda is _for_ in Python. I know what > they are, I know what the alternatives are, but I still haven't found > an instance where it permits something novel to be done that couldn't

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Andy Dingley
On 14 Feb, 16:23, Neil Cerutti <[EMAIL PROTECTED]> wrote: > str.translate is what I'd do. That's what I hope to do too, but it might not be possible (for the live, complex example). It looks as if I have to make a test, then process the contents of the code differently depending. There might well

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Martin P. Hellwig
Andy Dingley wrote: > I'm trying to write rot13, but to do it in a better and more Pythonic > style than I'm currrently using. What would you reckon to the > following pretty ugly thing? How would you improve it? In > particular, I don't like the way a three-way sel

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Neil Cerutti
On 2007-02-14, Andy Dingley <[EMAIL PROTECTED]> wrote: > I'm trying to write rot13, but to do it in a better and more > Pythonic style than I'm currrently using. What would you > reckon to the following pretty ugly thing? How would you > improve it? In particular,

Re: rot13 in a more Pythonic style?

2007-02-14 Thread Rune Strand
You could try "some_string".encode('rot_13') -- http://mail.python.org/mailman/listinfo/python-list

rot13 in a more Pythonic style?

2007-02-14 Thread Andy Dingley
I'm trying to write rot13, but to do it in a better and more Pythonic style than I'm currrently using. What would you reckon to the following pretty ugly thing? How would you improve it? In particular, I don't like the way a three-way selection is done by nesting two binary sel

Re: Pythonic style involves lots of lightweight classes (for me)

2006-12-14 Thread bayerj
Hi, I think that tuples are the best and simplest approach for small structures. >>> songs = [("Paranoid", "http://...";), ("Christian Woman", "http://...";)] >>> for title, url in songs: ... print "%s: %s" % (title, url) ... Paranoid: http://... Christian Woman: http://... I think that python'

Re: Pythonic style involves lots of lightweight classes (for me)

2006-12-14 Thread Andrea Griffini
metaperl wrote: > The above program started out as a list of dictionaries, but I > like the current approach much better. There is even a common idiom for this... class Record(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) This way you can use user

Pythonic style involves lots of lightweight classes (for me)

2006-12-13 Thread metaperl
I find it arduous to type dictionary['key'] and also feel that any data I create for a program deserves to have its operations tied to it. As a result, I often create lots of lightweight classes. Here's a small example: vlc = '/Applications/VLC.app/Contents/MacOS/VLC' class song(object): def