Re: Pygobject style question

2020-08-02 Thread Cameron Simpson
On 01Aug2020 13:32, Chris Green wrote: >Having (after lots of help from here, thank you) finally converted my >Python 2, gtk 2 program to Python 3 and pygobject gtk 3 I'm left with >a couple of what might be called style questions. > >I guess it's mostly down to what feels right to me but there ma

Pygobject style question

2020-08-01 Thread Chris Green
Having (after lots of help from here, thank you) finally converted my Python 2, gtk 2 program to Python 3 and pygobject gtk 3 I'm left with a couple of what might be called style questions. I guess it's mostly down to what feels right to me but there may be good reasons for choosing one way over a

Re: Package setup best practice style question

2016-05-28 Thread Steven D'Aprano
On Sun, 29 May 2016 03:00 am, Steven D'Aprano wrote: > On Sun, 29 May 2016 02:15 am, Gerald Britton wrote: > >> suppose I have a simple python project setup like this: [...] To which I responded: > If this is a single project, why do you set it up like this? Is there a > reason why you don't ju

Re: Package setup best practice style question

2016-05-28 Thread Steven D'Aprano
On Sun, 29 May 2016 02:15 am, Gerald Britton wrote: > suppose I have a simple python project setup like this: > > Project diectory > prog.py > pkg directory > __init__.py > mod1.py >class A: If this is a single project, why do you set it up like this

Package setup best practice style question

2016-05-28 Thread Gerald Britton
suppose I have a simple python project setup like this: Project diectory prog.py pkg directory __init__.py mod1.py class A: In order to have class A (unqualified) available from prog.py, there are a few options that I know about. I'm currently conside

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 9:10 AM, Wolfgang Maier wrote: > which I read as there has been a stepwise transition between 2.5 and 2.7 so > that 2.7 now behaves like Python 3 even without the __future__ statement. > OTOH, I believe you, of course, if you're saying implicit relative imports > are working

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Wolfgang Maier
On 04.12.2014 22:30, Chris Angelico wrote: On Fri, Dec 5, 2014 at 7:56 AM, Wolfgang Maier wrote: On 04.12.2014 19:05, Chris Angelico wrote: With os.path it definitely is. With the actual code in question, it's a Python 2.7 project that mostly uses relative imports - inside package.module1 is

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 7:56 AM, Wolfgang Maier wrote: > On 04.12.2014 19:05, Chris Angelico wrote: >> >> >> With os.path it definitely is. With the actual code in question, it's >> a Python 2.7 project that mostly uses relative imports - inside >> package.module1 is "import module2" etc - and I wa

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Wolfgang Maier
On 04.12.2014 19:05, Chris Angelico wrote: With os.path it definitely is. With the actual code in question, it's a Python 2.7 project that mostly uses relative imports - inside package.module1 is "import module2" etc - and I was writing an external script that calls on one of the modules. What

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Chris Angelico
On Fri, Dec 5, 2014 at 4:36 AM, Jean-Michel Pichavant wrote: > I know you specifically stated you didn't want to do this but > > import os > > os.path.isfile() > > is the best option imo, especially from the reader point of view ("Namespaces > are one honking great idea"). With os.path it defini

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Ethan Furman
On 12/03/2014 03:02 AM, Chris Angelico wrote: > > Throughout the code, I want to refer to "path.split()", > "path.isfile()", etc, without the "os." in front of them. I could do > either of these: > > import os.path as path > from os import path > > Which one would you recommend? Does it depend o

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Ethan Furman
On 12/04/2014 09:36 AM, Jean-Michel Pichavant wrote: > > I know you specifically stated you didn't want to do this but > > import os > > os.path.isfile() > > is the best option imo, especially from the reader point of view ("Namespaces > are one honking great idea"). But, "Flat is better

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Jean-Michel Pichavant
- Original Message - > From: "Chris Angelico" > To: python-list@python.org > Sent: Wednesday, 3 December, 2014 12:02:17 PM > Subject: Style question: Importing modules from packages - 'from' vs 'as' > > When importing a module from a subpac

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-04 Thread Wolfgang Maier
On 12/03/2014 12:02 PM, Chris Angelico wrote: When importing a module from a subpackage, it's sometimes convenient to refer to it throughout the code with a one-part name rather than two. I'm going to use 'os.path' for the examples, but my actual use-case is a custom package where the package nam

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-03 Thread Terry Reedy
On 12/3/2014 6:02 AM, Chris Angelico wrote: When importing a module from a subpackage, it's sometimes convenient to refer to it throughout the code with a one-part name rather than two. I'm going to use 'os.path' for the examples, but my actual use-case is a custom package where the package name

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-03 Thread Ian Kelly
On Dec 3, 2014 4:34 AM, "Chris Angelico" wrote: > > On Wed, Dec 3, 2014 at 10:27 PM, Peter Otten <__pete...@web.de> wrote: > > Don't repeat yourself, so > > > > from os import path > > > > always. On the other hand I have never thought about actual renames, e. g. > > > > from os import path as std

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-03 Thread Chris Angelico
On Wed, Dec 3, 2014 at 10:27 PM, Peter Otten <__pete...@web.de> wrote: > Don't repeat yourself, so > > from os import path > > always. On the other hand I have never thought about actual renames, e. g. > > from os import path as stdpath > > versus > > import os.path as stdpath > > I think I'd use t

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-03 Thread Peter Otten
Chris Angelico wrote: > When importing a module from a subpackage, it's sometimes convenient > to refer to it throughout the code with a one-part name rather than > two. I'm going to use 'os.path' for the examples, but my actual > use-case is a custom package where the package name is, in the > ap

Re: Style question: Importing modules from packages - 'from' vs 'as'

2014-12-03 Thread Tim Delaney
On 3 December 2014 at 22:02, Chris Angelico wrote: > > import os.path as path > from os import path > Bah - deleted the list and sent directly to Chris ... time to go to bed. The advantage of the former is that if you want to use a different name, it's a smaller change. But the disadvantage of

Style question: Importing modules from packages - 'from' vs 'as'

2014-12-03 Thread Chris Angelico
When importing a module from a subpackage, it's sometimes convenient to refer to it throughout the code with a one-part name rather than two. I'm going to use 'os.path' for the examples, but my actual use-case is a custom package where the package name is, in the application, quite superfluous. Th

Re: Python Style Question

2014-11-12 Thread Anton
On Thursday, October 30, 2014 4:10:23 AM UTC-7, Steven D'Aprano wrote: > I don't particularly like either version. I prefer this: > > def load_int(obj): > if isinstance(obj, int): > # Case 1), an int, e.g. 7 > return obj > elif isinstance(obj, str): > # Case 2) and

Re: Python Style Question

2014-10-30 Thread Denis McMahon
On Fri, 31 Oct 2014 09:48:10 +1100, Steven D'Aprano wrote: > MRAB wrote: >> How about: >> >> int(str(obj).strip('"')) > > Absolutely not. > > obj = '""1\n\n\n\n' # not valid JSON load_int(obj) > => raises ValueError int(str(obj).strip('"')) > => wrongly returns 1 How about #!/us

Re: Python Style Question

2014-10-30 Thread Steven D'Aprano
Roy Smith wrote: > In article <54521c8f$0$12982$c3e8da3$54964...@news.astraweb.com>, > Steven D'Aprano wrote: > >> Anton wrote: >> >> > Let's say I have an incoming list of values *l*. Every element of *l* >> > can be one of the following options: >> > 1) an integer value >> > 2) a string in f

Re: Python Style Question

2014-10-30 Thread Roy Smith
In article <54521c8f$0$12982$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > Anton wrote: > > > Let's say I have an incoming list of values *l*. Every element of *l* can > > be one of the following options: > > 1) an integer value > > 2) a string in form of '', e.g. '7' > > 3) a

Re: Python Style Question

2014-10-30 Thread Steven D'Aprano
MRAB wrote: > On 2014-10-30 11:10, Steven D'Aprano wrote: >> Anton wrote: >> >>> Let's say I have an incoming list of values *l*. Every element of *l* >>> can be one of the following options: >>> 1) an integer value >>> 2) a string in form of '', e.g. '7' >>> 3) a string with a json serialization

Re: Python Style Question

2014-10-30 Thread MRAB
On 2014-10-30 11:10, Steven D'Aprano wrote: Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of '', e.g. '7' 3) a string with a json serialization of an integer value, e.g. '"7"' 4) so

Re: Python Style Question

2014-10-30 Thread Steven D'Aprano
Anton wrote: > Let's say I have an incoming list of values *l*. Every element of *l* can > be one of the following options: > 1) an integer value > 2) a string in form of '', e.g. '7' > 3) a string with a json serialization of an integer value, e.g. '"7"' > 4) something else that should be ignor

Re: Python Style Question

2014-10-29 Thread Anton
On Wednesday, October 29, 2014 4:59:25 AM UTC-7, Rafael Romero Carmona wrote: > 2014-10-29 12:25 GMT+01:00 Martin Kemp : > Actually it doesn't work because there is no add function and it > doesn't catch the TypeError function to ignore other exceptions than > ValueError. Doesn't it? I tested in Py

Re: Python Style Question

2014-10-29 Thread Anton
On Wednesday, October 29, 2014 4:43:33 AM UTC-7, Rafael Romero Carmona wrote: > Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add > function but they have append. You are right, in my original code I use set instead of array, so it should be either values = set() or values.append

Re: Python Style Question

2014-10-29 Thread Martin Kemp
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 29/10/2014 12:01, Rafael Romero Carmona wrote: > 2014-10-29 12:25 GMT+01:00 Martin Kemp : On > 29/10/2014 10:45, Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an

Re: Python Style Question

2014-10-29 Thread Rafael Romero Carmona
2014-10-29 12:25 GMT+01:00 Martin Kemp : > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > On 29/10/2014 10:45, Anton wrote: >> Let's say I have an incoming list of values *l*. Every element of >> *l* can be one of the following options: 1) an integer value 2) a >> string in form of '', e.g. '7

Re: Python Style Question

2014-10-29 Thread Rafael Romero Carmona
Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add function but they have append. I think you could do better with something like == import json l = [1, -1, 0, '1', '-1', '0', json.dumps(-1), json.dumps(1), json.dumps(0), 'x', 'sqjklsqjk__', (1, 2)] values = [] for c in

Re: Python Style Question

2014-10-29 Thread Martin Kemp
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 29/10/2014 10:45, Anton wrote: > Let's say I have an incoming list of values *l*. Every element of > *l* can be one of the following options: 1) an integer value 2) a > string in form of '', e.g. '7' 3) a string with a json > serialization of an int

Python Style Question

2014-10-29 Thread Anton
Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of '', e.g. '7' 3) a string with a json serialization of an integer value, e.g. '"7"' 4) something else that should be ignored I need to transform th

Re: Style question -- plural of class name?

2013-05-09 Thread Robert Kern
On 2013-05-08 21:20, Roy Smith wrote: FooEntry is a class. How would you describe a list of these in a docstring? "A list of FooEntries" "A list of FooEntrys" "A list of FooEntry's" "A list of FooEntry instances" The first one certainly sounds the best, but it seems wierd to change the spel

Re: Style question -- plural of class name?

2013-05-09 Thread Neil Cerutti
On 2013-05-09, Jussi Piitulainen wrote: > Neil Cerutti writes: >> If there's no chance for confusion between a class named >> FooEntry and another named FooEntries, then the first attempt >> seems best. Pluralize a class name by following the usual >> rules, e.g., "strings" and "ints". > > Like "s

Re: Style question -- plural of class name?

2013-05-09 Thread Jussi Piitulainen
Neil Cerutti writes: > If there's no chance for confusion between a class named FooEntry > and another named FooEntries, then the first attempt seems best. > Pluralize a class name by following the usual rules, e.g., > "strings" and "ints". Like "strings" would be "foo entries". Which might work

Re: Style question -- plural of class name?

2013-05-09 Thread Neil Cerutti
On 2013-05-08, Denis McMahon wrote: > On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: > >> FooEntry is a class. How would you describe a list of these in a >> docstring? >> >> "A list of FooEntries" >> >> "A list of FooEntrys" >> >> "A list of FooEntry's" >> >> "A list of FooEntry instan

Re: Style question -- plural of class name?

2013-05-09 Thread Thomas Rachel
Am 09.05.2013 02:38 schrieb Colin J. Williams: On 08/05/2013 4:20 PM, Roy Smith wrote: "A list of FooEntry's" +1 Go back to school. Both of you... That is NOT the way to build a plural form... Thomas -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question -- plural of class name?

2013-05-08 Thread Cameron Simpson
On 09May2013 00:02, Steven D'Aprano wrote: | On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: | > "A list of FooEntry's" | | "Here come's an S! Quick, jam on an apostrophe!" | | This is called the grocer's apostrophe, and is universally held in | contempt no matter what variant of English

Re: Style question -- plural of class name?

2013-05-08 Thread Colin J. Williams
On 08/05/2013 4:20 PM, Roy Smith wrote: FooEntry is a class. How would you describe a list of these in a docstring? "A list of FooEntries" 0 "A list of FooEntrys" -1 "A list of FooEntry's" +1 "A list of FooEntry instances" No FooEntry is specified as a class. The first one certainly so

Re: Style question -- plural of class name?

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 15:33:07 -0500, Skip Montanaro wrote: > This one: > >> "A list of FooEntry instances" > > Besides the obvious spelling issues in the others, it's not immediately > clear if the list contains just FooEntry instances, FooEntry classes > (perhaps subclasses) or a mix of the two.

Re: Style question -- plural of class name?

2013-05-08 Thread Chris Angelico
On Thu, May 9, 2013 at 6:20 AM, Roy Smith wrote: > "A list of FooEntry's" Only if you put another apostrophe in: "A list of 'FooEntry's" But the delimited style is almost never of use. I'd go for this only if there were some sort of automated markup being applied - if the word FooEntry were tur

Re: Style question -- plural of class name?

2013-05-08 Thread Steven D'Aprano
On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? Which language are you writing your docstrings in? Obey the normal rules of spelling, grammar and punctuation for your language, which I assume is English. >

Re: Style question -- plural of class name?

2013-05-08 Thread Denis McMahon
On Wed, 08 May 2013 16:20:48 -0400, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? > > "A list of FooEntries" > > "A list of FooEntrys" > > "A list of FooEntry's" > > "A list of FooEntry instances" > > The first one certainly sounds the best,

Re: Style question -- plural of class name?

2013-05-08 Thread Ian Kelly
On Wed, May 8, 2013 at 2:37 PM, John Downs wrote: > On Wed, May 8, 2013 at 4:20 PM, Roy Smith wrote: >> >> FooEntry is a class. How would you describe a list of these in a >> docstring? >> >> "A list of FooEntries" >> >> "A list of FooEntrys" >> >> "A list of FooEntry's" >> >> "A list of FooEntr

Re: Style question -- plural of class name?

2013-05-08 Thread John Downs
On Wed, May 8, 2013 at 4:20 PM, Roy Smith wrote: > FooEntry is a class. How would you describe a list of these in a > docstring? > > "A list of FooEntries" > > "A list of FooEntrys" > > "A list of FooEntry's" > > "A list of FooEntry instances" > > The first one certainly sounds the best, but it

Re: Style question -- plural of class name?

2013-05-08 Thread Skip Montanaro
This one: > "A list of FooEntry instances" Besides the obvious spelling issues in the others, it's not immediately clear if the list contains just FooEntry instances, FooEntry classes (perhaps subclasses) or a mix of the two. #4 makes it clear. Skip -- http://mail.python.org/mailman/listinfo/p

Style question -- plural of class name?

2013-05-08 Thread Roy Smith
FooEntry is a class. How would you describe a list of these in a docstring? "A list of FooEntries" "A list of FooEntrys" "A list of FooEntry's" "A list of FooEntry instances" The first one certainly sounds the best, but it seems wierd to change the spelling of the class name to make it plural

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Steven D'Aprano
On Tue, 17 Jul 2012 05:23:22 -0700, Michele Simionato wrote: > The standard is to use `cls`. In the __new__ method you can use `mcl` or > `meta`. Thanks to everyone who answered. I think I will stick with "meta" and "cls". -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Ian Kelly
On Tue, Jul 17, 2012 at 12:10 AM, alex23 wrote: > On Jul 17, 1:29 am, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> Here's a style question for you: in a metaclass, what should I call the >> instance parameter of methods, "cls" or "

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Ian Kelly
On Tue, Jul 17, 2012 at 6:23 AM, Michele Simionato wrote: > The standard is to use `cls`. In the __new__ method you can use `mcl` or > `meta`. I've also seen `mcs` a fair amount. -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: metaclass self vs cls?

2012-07-17 Thread Michele Simionato
The standard is to use `cls`. In the __new__ method you can use `mcl` or `meta`. -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: metaclass self vs cls?

2012-07-16 Thread Terry Reedy
On 7/16/2012 11:29 AM, Steven D'Aprano wrote: Here's a style question for you: in a metaclass, what should I call the instance parameter of methods, "cls" or "self"? class ExampleMeta(type): def method(self, *args): ... I'm not quite sure if that feels

Style question: metaclass self vs cls?

2012-07-16 Thread Steven D'Aprano
Here's a style question for you: in a metaclass, what should I call the instance parameter of methods, "cls" or "self"? class ExampleMeta(type): def method(self, *args): ... I'm not quite sure if that feels right. On the one hand, self is the ExampleMeta

Re: Style question (Poll)

2012-03-15 Thread Jon Clements
On Wednesday, 14 March 2012 21:16:05 UTC, Terry Reedy wrote: > On 3/14/2012 4:49 PM, Arnaud Delobelle wrote: > > On 14 March 2012 20:37, Croepha wrote: > >> Which is preferred: > >> > >> for value in list: > >> if not value is another_value: > >> value.do_something() > >> break > > Do

Re: Style question (Poll)

2012-03-14 Thread Chris Angelico
On Thu, Mar 15, 2012 at 7:37 AM, Croepha wrote: > Which is preferred: > > for value in list: >  if not value is another_value: >    value.do_something() >    break > > --or-- > > if list and not list[0] is another_value: >  list[0].do_something() > > Comments are welcome, Thanks General principle

RE: Style question (Poll)

2012-03-14 Thread Prasad, Ramit
> > Only use 'is' if you are looking for objects like True, > > False, None or something that MUST be exactly the same object. > > I've rarely seen valid uses of 'is True' or 'is False'. It can be useful when you think something might be None or False. Although, I suppose you could always just us

Re: Style question (Poll)

2012-03-14 Thread Arnaud Delobelle
On 14 March 2012 22:15, Prasad, Ramit wrote: > Only use 'is' if you are looking for objects like True, > False, None or something that MUST be exactly the same object. I've rarely seen valid uses of 'is True' or 'is False'. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list

RE: Style question (Poll)

2012-03-14 Thread Prasad, Ramit
> >> Which is preferred: > >> > >> for value in list: > >> if not value is another_value: > >> value.do_something() > >> break > > Do you really mean 'is' or '=='? Let me expound on how 'is' and '==' are very different. It may work for some comparisons but often not for others. Certain

Re: Style question (Poll)

2012-03-14 Thread Terry Reedy
On 3/14/2012 4:49 PM, Arnaud Delobelle wrote: On 14 March 2012 20:37, Croepha wrote: Which is preferred: for value in list: if not value is another_value: value.do_something() break Do you really mean 'is' or '=='? If you mean x is not y, write it that way. 'not x is y' can be mis

Re: Style question (Poll)

2012-03-14 Thread Arnaud Delobelle
On 14 March 2012 20:37, Croepha wrote: > Which is preferred: > > for value in list: >  if not value is another_value: >    value.do_something() >    break > > --or-- > > if list and not list[0] is another_value: >  list[0].do_something() Hard to say, since they don't do the same thing :) I suspe

Style question (Poll)

2012-03-14 Thread Croepha
Which is preferred: for value in list: if not value is another_value: value.do_something() break --or-- if list and not list[0] is another_value: list[0].do_something() Comments are welcome, Thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: Style question: Nicknames for deeply nested objects

2011-02-03 Thread Jean-Michel Pichavant
Gerald Britton wrote: Nope. it's nothing to do with imports. It's about objects passed to methods at run time. Complicated objects with many levels. Not about modules at all. Who is providing these objects ? - Your code ? => as said before, you can fix your design with a proper object

Re: Style question: Nicknames for deeply nested objects

2011-02-03 Thread Jean-Michel Pichavant
Gerald Britton wrote: however, considering what "import a.module.that.is.quite.nested as myModule" Won't work since I get the objects at run time myModule = __import__('whatever.module.imported.at.run.time', globals(), locals(), [], -1) See http://docs.python.org/library/function

Re: Style question: Nicknames for deeply nested objects

2011-01-31 Thread Jean-Michel Pichavant
Gerald Britton wrote: Hi all, Today I was thinking about a problem I often encounter. [snip] 1. You need to call this thing many times with different arguments, so you wind up with: x = some.deeply.nested.object.method(some.other.deeply.nested.object.value1) y = some.deeply.nested.obj

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Stephen Hansen
On 1/30/11 1:13 PM, rantingrick wrote: > On Jan 30, 12:53 pm, Stephen Hansen wrote: >> OH MY GOD. How can someone be expected to understand what a function does! > > Yes, and also how decorators word and generators work, and ... > >> Be serious! You can't expect that of them. > > I don't. I don

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Steven D'Aprano
On Sun, 30 Jan 2011 12:51:20 -0500, Gerald Britton wrote: > Hi all, > > Today I was thinking about a problem I often encounter.  Say that I have > (seems I often do!) a deeply nested object, by which I mean object > within object with object, etc. > > For example: > >    x = >    some.deeply.ne

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Jerry Hill
> > I don't. I don't expect anyone to write 10 lines of obfuscation code > when just two will suffice. Maybe you should join the perl group as > they would proud! But Stephen's 10 lines of somewhat obscure code actually works, and your two lines of code doesn't. I know which one I would prefer.

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread rantingrick
On Jan 30, 12:53 pm, Stephen Hansen wrote: > On 1/30/11 10:35 AM, rantingrick wrote: > > > Well congratulations Stephen, you win the obfuscation prize of the > > year! > > Yes, > > On 1/30/11 10:09 AM, rantingrick wrote: > > > Here is how a pythonic local block would look > > > with this as localv

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Ian
On 30/01/2011 17:51, Gerald Britton wrote: Hi all, Today I was thinking about a problem I often encounter. Say that I have (seems I often do!) a deeply nested object, by which I mean object within object with object, etc. For example: x = some.deeply.nested.object.method(some.other.deeply

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Stephen Hansen
On 1/30/11 10:35 AM, rantingrick wrote: > Well congratulations Stephen, you win the obfuscation prize of the > year! Yes, On 1/30/11 10:09 AM, rantingrick wrote: > Here is how a pythonic local block would look > > with this as localvar: > localvar.do_something() verses with my(this) as loca

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread rantingrick
On Jan 30, 12:23 pm, Stephen Hansen wrote: > --- start > from contextlib import contextmanager > > class Item(object): pass > > deeply = Item() > deeply.nested = Item() > deeply.nested.thing = Item() > > @contextmanager > def my(thing): >     yield thing > > with my(deeply.nested.thing) as o: >  

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Stephen Hansen
On 1/30/11 9:51 AM, Gerald Britton wrote: > 1. If you had to choose between approaches 1 and 2, which one would > you go for, and why? Neither. Ideally, I'd tweak the API around so the deeply nested structure isn't something I need to access regularly. But! If you can't do that, I'd do something l

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread Roy Smith
In article , Gerald Britton wrote: > 1. You need to call this thing many times with different arguments, so > you wind up with: > >    x = > some.deeply.nested.object.method(some.other.deeply.nested.object.value1) >    y = > some.deeply.nested.object.method(some.other.deeply.nested.object.val

Re: Style question: Nicknames for deeply nested objects

2011-01-30 Thread rantingrick
On Jan 30, 11:51 am, Gerald Britton wrote: [...] > that I might confuse with the first.  To make it look better I might do this: > >    _o = some.deeply.nested.object >    _o.method(_o.value) > > which is fine, I suppose. It is very fine. And you "supposed" correctly! > Then, putting on my co

Style question: Nicknames for deeply nested objects

2011-01-30 Thread Gerald Britton
Hi all, Today I was thinking about a problem I often encounter.  Say that I have (seems I often do!) a deeply nested object, by which I mean object within object with object, etc. For example:    x = some.deeply.nested.object.method(some.other.deeply.nested.object.value) Well, that's extreme bu

Re: If/then style question

2010-12-21 Thread Francesco
I'd bet you would stress your point Steven! But you don't need to persuade me, I do already agree. I just meant to say that, when the advantage is little, there's no need to rewrite a working function. And that with modern CPUs, if tests take so little time, that even some redundant one is not

Re: If/then style question

2010-12-19 Thread Steven D'Aprano
On Sat, 18 Dec 2010 19:59:45 -0800, Carl Banks wrote: > On Dec 17, 12:23 am, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: >> > Even without the cleanup issue, sometimes you want to edit a function >> > to affect all return values

Re: If/then style question

2010-12-18 Thread Carl Banks
On Dec 17, 12:23 am, Steven D'Aprano wrote: > On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: > > Even without the cleanup issue, sometimes you want to edit a function to > > affect all return values somehow.  If you have a single exit point you > > just make the change there; if you have mu

Re: If/then style question

2010-12-18 Thread Steven D'Aprano
On Sat, 18 Dec 2010 12:29:31 +0100, Francesco wrote: [...] > I agree to your point, but I'm afraid you chose a wrong example (AFAIK, > and that's not much). Sure, the second version of function(arg) is much > more readable, but why do you think the first one would do "*lots* of > unnecessary work

Re: If/then style question

2010-12-18 Thread Francesco
On 17/12/2010 0.51, Steven D'Aprano wrote: Don't get me wrong... spaghetti code is*bad*. But there are other ways of writing bad code too, and hanging around inside a function long after you've finished is also bad: def function(arg): done = False do_something() if some_condition:

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 17:26:08 +, Grant Edwards wrote: > Give me code that's easy-to-read and doesn't work rather code that works > and can't be read any day. Well, in that case, you'll love my new operating system, written in 100% pure Python: [start code] print("this is an operating system"

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 10:53:45 -0500, Steve Holden wrote about for...else: > This construct appears to be unpopular in actual use, and when it comes > up in classes and seminars there is always interesting debate as people > discuss potential uses and realise there are useful applications. Yes, I f

Re: If/then style question

2010-12-17 Thread Kev Dwyer
On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: > (This is mostly a style question, and perhaps one that has already been > discussed elsewhere. If so, a pointer to that discussion will be > appreciated!) > > When I started learning Python, I wrote a lot of methods tha

RE: If/then style question

2010-12-17 Thread Rob Richardson
-Original Message- You have outlined what happens when cond1 and cond2 both evaluate to True -- what happens if, say, cond2 evaluates to False? - I reply And the light goes on! (And palm strikes forehead.) I was thinking that the error we were processing was raised by

Re: If/then style question

2010-12-17 Thread Grant Edwards
On 2010-12-16, Steven D'Aprano wrote: > On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: > >> (This is mostly a style question, and perhaps one that has already been >> discussed elsewhere. If so, a pointer to that discussion will be >> appreciated!) >>

Re: If/then style question

2010-12-17 Thread Grant Edwards
On 2010-12-16, Stefan Sonnenberg-Carstens wrote: > The advantage in latter case is fewer operations, because you can > skip the assignments, and it is more readable. > > The "one entry, one exit" is an advice. Not a law. > Your code is OK. > > As long as it works ;-) Even that last bit isn't th

Re: If/then style question

2010-12-17 Thread python
> I now remember this idiom as the "break else" construct: either the loop > breaks, or the "else:" suite is executed. A perfect description. Malcolm -- http://mail.python.org/mailman/listinfo/python-list

Re: If/then style question

2010-12-17 Thread Arnaud Delobelle
Tim Golden writes: > On 17/12/2010 15:53, Steve Holden wrote: > > [... snip example of for-else ...] > >> This construct appears to be unpopular in actual use, and when it comes >> up in classes and seminars there is always interesting debate as people >> discuss potential uses and realise there

Re: If/then style question

2010-12-17 Thread Mark Wooding
Steve Holden writes: > I think the choice of keyword is probably not Guido's crowning > language achievement, I remember the behaviour by considering a typical application: for thing in things: if shinyp(thing): break else: raise DullError, 'nothi

Re: If/then style question

2010-12-17 Thread Steve Holden
On 12/17/2010 11:13 AM, Tim Golden wrote: > On 17/12/2010 15:53, Steve Holden wrote: > > [... snip example of for-else ...] > >> This construct appears to be unpopular in actual use, and when it comes >> up in classes and seminars there is always interesting debate as people >> discuss potential

Re: If/then style question

2010-12-17 Thread Ethan Furman
Rob Richardson wrote: My thanks for pointing out the existence of the else: suite in the for statement. However, I remain confused. For reference, here's the original code: def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'),

Re: If/then style question

2010-12-17 Thread Paul Rubin
Jean-Michel Pichavant writes: > What about, > > def myMethod(): >for condition, exitCode in [ >(cond1, 'error1'), >(cond2, 'very bad error'), >]: >if not condition: >break >else: > do_some_usefull_stuff() # executed only if the we never

Re: If/then style question

2010-12-17 Thread Tim Golden
On 17/12/2010 15:53, Steve Holden wrote: [... snip example of for-else ...] This construct appears to be unpopular in actual use, and when it comes up in classes and seminars there is always interesting debate as people discuss potential uses and realise there are useful applications. I use t

RE: If/then style question

2010-12-17 Thread Rob Richardson
My thanks for pointing out the existence of the else: suite in the for statement. However, I remain confused. For reference, here's the original code: > def myMethod(): > for condition, exitCode in [ > (cond1, 'error1'), > (cond2, 'very bad error'), > ]: >

Re: If/then style question

2010-12-17 Thread Steve Holden
On 12/17/2010 9:38 AM, Steven D'Aprano wrote: > On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote: > > >> First, just to clarify, I don't think the indentation I saw was what was >> originally posted. The "else" must be indented to match the "if", and >> the two statements under "else" ar

Re: If/then style question

2010-12-17 Thread David Robinow
On Thu, Dec 16, 2010 at 6:51 PM, Steven D'Aprano wrote: ... > Functions always have one entry. The only way to have multiple entry > points is if the language allows you to GOTO into the middle of a > function, and Python sensibly does not allow this. The "one entry, one > exit" rule comes from th

Re: If/then style question

2010-12-17 Thread Jean-Michel Pichavant
Rob Richardson wrote: -Original Message- What about, def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if not condition: break else: do_some_usefull_stuff() # executed only if the

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote: > First, just to clarify, I don't think the indentation I saw was what was > originally posted. The "else" must be indented to match the "if", and > the two statements under "else" are in the else block. The return > statement is indente

  1   2   3   >