Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7789

Re: break in a module

Date 2011-06-16 18:21 -0700
From Erik Max Francis <max@alcyone.com>
Organization Alcyone Systems
Newsgroups comp.lang.python
Subject Re: break in a module
References (3 earlier) <qaWdnQmQzemLaWrQnZ2dnUVZ5vidnZ2d@giganews.com> <mailman.247.1308099095.11593.python-list@python.org> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> <mailman.51.1308272276.1164.python-list@python.org>
Message-ID <VOGdnS1-M-W9MWfQnZ2dnUVZ5vSdnZ2d@giganews.com> (permalink)

Show all headers | View raw


Chris Angelico wrote:
> On Fri, Jun 17, 2011 at 10:48 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> Perhaps the most sensible alternative is conditional importing:
>>
>> # === module extras.py ===
>>
>> def ham(): pass
>> def cheese(): pass
>> def salad(): pass
>>
>>
>> # === module other.py ===
>>
>> def spam(): pass
>>
>> if not some_condition: from extras import *
>>
> 
> This would, if I understand imports correctly, have ham() operate in
> one namespace and spam() in another. Depending on what's being done,
> that could be quite harmless, or it could be annoying (no sharing
> module-level constants, etc).

No, he's using `from ... import *`.  It dumps it all in the same 
namespace (which is usually why it's frowned upon, but in his example 
it's kind of the point).

I don't see it as a very compelling solution beyond putting code using a 
simple `if`.  And it's still rather hard to imagine a serious use case.

> As to which keyword is used, I would be inclined to go with 'return'
> rather than 'break'. The module is thus a procedure in its own right.
> Of course, that's assuming the feature's actually needed, which isn't
> certain by any means.

Neither makes sense.  `break` exits out of looping structures, which the 
top-level code of a module most certainly is not.  `return` returns out 
of functions or methods and -- most importantly -- _returns something_. 
  (`return` without an argument returns None).  Modules have no facility 
to return anything, nor would it make much sense at all to do so.

This is an example of a problem that simply does not need solving with 
additional language changes:  There are already perfectly good (and, 
perhaps more importantly, clear) facilities to do whatever it is you 
want to do.  If you want to exit, call `sys.exit`.  If you want to 
conditionally execute some code, use `if`.  If you want to indicate an 
exceptional condition, raise an exception.

-- 
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   Human salvation lies in the hands of the creatively maladjusted.
    -- Dr. Martin Luther King, Jr.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: break in a module Ethan Furman <ethan@stoneleaf.us> - 2011-06-14 16:28 -0700
  Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-14 16:51 -0700
    Re: break in a module Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-14 18:51 -0600
      Re: break in a module Ben Finney <ben+python@benfinney.id.au> - 2011-06-15 11:33 +1000
        Re: break in a module Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-14 20:21 -0600
          Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 15:09 -0700
        Re: break in a module Dave Angel <davea@ieee.org> - 2011-06-15 00:02 -0400
      Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 15:07 -0700
        Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-17 09:27 +1000
          Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 16:29 -0700
            Re: break in a module Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-16 18:00 -0600
            Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-17 10:01 +1000
              Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 18:13 -0700
                Re: break in a module Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 19:17 -0700
                Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 21:21 -0700
                Re: break in a module Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-16 22:53 -0600
        Re: break in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-17 00:48 +0000
          Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-17 10:57 +1000
            Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 18:21 -0700
              Re: break in a module Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 19:11 -0700
              Re: break in a module Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-16 19:58 -0600
                Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 21:24 -0700
                Re: break in a module Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-16 22:50 -0600
                Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-16 22:20 -0700
                Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-17 15:56 +1000
                Re: break in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-17 06:00 +0000
                Re: break in a module Erik Max Francis <max@alcyone.com> - 2011-06-17 00:09 -0700
                Re: break in a module Cameron Simpson <cs@zip.com.au> - 2011-06-18 12:36 +1000
                Re: break in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-18 03:50 +0000
                Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-18 14:31 +1000
                Re: break in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-18 04:49 +0000
                Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-18 15:06 +1000
                Re: break in a module Cameron Simpson <cs@zip.com.au> - 2011-06-21 20:04 +1000
                Re: break in a module Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-17 00:25 -0600
    Re: break in a module Chris Angelico <rosuav@gmail.com> - 2011-06-15 11:33 +1000

csiph-web