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


Groups > comp.lang.python > #12906

Re: How to structure packages

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'defines': 0.07; 'unittest': 0.07; 'files:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'package.': 0.12; 'packages.': 0.13; 'disadvantage': 0.16; 'filenames': 0.16; 'myclass': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'two.': 0.16; 'wrote:': 0.16; 'converted': 0.18; 'convert': 0.19; 'defined': 0.19; 'this?': 0.21; 'trying': 0.21; 'assume': 0.22; 'there.': 0.24; 'statement': 0.25; "i'm": 0.27; 'classes': 0.28; 'separate': 0.28; 'import': 0.28; 'module.': 0.29; 'example': 0.30; 'module': 0.30; 'from:addr:web.de': 0.30; 'yes.': 0.30; 'class': 0.30; 'there': 0.33; 'to:addr:python-list': 0.33; 'rule': 0.34; 'file.': 0.34; 'header:X-Complaints-To:1': 0.35; 'rather': 0.35; 'subject:How': 0.35; 'file': 0.36; 'another': 0.37; 'class.': 0.37; 'but': 0.37; 'could': 0.38; 'received:org': 0.38; 'move': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'goes': 0.39; 'define': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'more': 0.60; 'here': 0.65; 'mechanical': 0.67; 'package?': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to structure packages
Date Wed, 07 Sep 2011 19:30:34 +0200
Organization None
References <2a4f542c-a8c1-46c7-9899-a3fad0940cf6@x11g2000yqc.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084aa70.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.842.1315416661.27778.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315416661 news.xs4all.nl 2431 [2001:888:2000:d::a6]:58984
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12906

Show key headers only | View raw


bclark76 wrote:

> I'm learning python, and was playing with structuring packages.

If you are coming from Jave you have to unlearn a thing or two.
 
> Basically I want to have a package called mypackage that defines a
> number of classes and functions.

> I'm trying to follow the rule that every file defines only one class.
> I could define MyClass in __init__.py, but then what if I wanted to
> define more classes in the mypackage package? My one class per file
> rule goes out the window.
> 
> Is this rule wrongheaded, 

Yes. 

> or is there another way to do this?

I recommend that you always start out with a module. Once that becomes 
unwieldy you can convert it into a package. Let's assume that

mystuff.py

contains a MyClass that you want to move into a separate file. You get the 
following files:

mystuff
    __init__.py
    descriptivename.py # MyClass here

Note that all filenames are lowercase. If you add the line

from .descriptivename import MyClass

to __init__.py you can continue to import and use MyClass with the statement

from mystuff import MyClass
m = MyClass()

or

import mystuff
m = mystuff.MyClass()

The disadvantage is of course that mystuff.descriptivename will always be 
imported, even if you don't need the part of the mystuff package defined 
there.

You may also have a look into unittest as an example of a module that was 
recently converted into a package. Classes and functions are grouped into 
submodules by their functionality rather than employing Java's mechanical 
one-class-per-file pattern.

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


Thread

How to structure packages bclark76 <bclark76@gmail.com> - 2011-09-07 08:56 -0700
  Re: How to structure packages John Gordon <gordon@panix.com> - 2011-09-07 16:11 +0000
    Re: How to structure packages Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-09-07 19:18 +0200
  Re: How to structure packages Peter Otten <__peter__@web.de> - 2011-09-07 19:30 +0200
    Re: How to structure packages Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-08 10:29 +1000
      Re: How to structure packages Chris Angelico <rosuav@gmail.com> - 2011-09-08 12:39 +1000
      Re: How to structure packages Dan Sommers <dan@tombstonezero.net> - 2011-09-08 09:51 +0000
      Re: How to structure packages Jonathan Hartley <tartley@tartley.com> - 2011-09-08 03:22 -0700
      Re: How to structure packages Nobody <nobody@nowhere.com> - 2011-09-09 01:45 +0100
        Re: How to structure packages Chris Angelico <rosuav@gmail.com> - 2011-09-09 11:37 +1000
          Re: How to structure packages Nobody <nobody@nowhere.com> - 2011-09-10 11:11 +0100
            Re: How to structure packages Chris Angelico <rosuav@gmail.com> - 2011-09-10 20:29 +1000
            Re: How to structure packages "Littlefield, Tyler" <tyler@tysdomain.com> - 2011-09-10 08:04 -0600
        Re: How to structure packages Alec Taylor <alec.taylor6@gmail.com> - 2011-09-10 02:38 +1000
  Re: How to structure packages rantingrick <rantingrick@gmail.com> - 2011-09-07 10:56 -0700
  Re: How to structure packages "Littlefield, Tyler" <tyler@tysdomain.com> - 2011-09-07 12:11 -0600
  Re: How to structure packages Westley Martínez <anikom15@gmail.com> - 2011-09-07 14:35 -0700

csiph-web