osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Import order question


Nagy L?szl? Zsolt wrote:

> I have a class hierarchy like this:
> 
> Widget <- VisualWidget <- BsWidget
> 
> and then BsWidget has many descendants: Desktop, Row, Column, Navbar etc.
> 
> Widgets can have children. They are stored in a tree. In order to manage
> the order of widgets, I need methods to append children. (And later:
> insert or prepend because they also have an order). So I would like to
> have methods like this:
> 
> BsWidget.AppendNavbar(....)
> BsWidget.AppendRow(...)
> 
> Here is the problem: these methods should create instances of Row,
> Column and Navbar. But this leads to circular imports.
> 
> Here is code for BsWidget:
> 
> from shopzeus.yaaf.ui.visualwidget import VisualWidget
> 
> from shopzeus.yaaf.ui.bootstrap.row import Row
> from shopzeus.yaaf.ui.bootstrap.column import Column
> from shopzeus.yaaf.ui.bootstrap.navbar import Navbar
> 
> class BsWidget(VisualWidget):
>      """Visual widget for bootstrap.
> 
>      Adds extra methods for adding/removing content like rows
>      columnsetc.""" def __init__(self,parent):
>          <more code here>
> 
>      def AppendRow(self):
>          return Row(self)
> 
>      def AppendColumn(self):
>          return Row(self)
> 
>      def PrependRow(self):
>          return Row(self,position=-1)
> 
>      <more code here>
> 
> 
> Here is code for ClassX (where ClassX can be: Row, Column, Desktop,
> Navbar etc.):
> 
> from shopzeus.yaaf.ui.bootstrap.bswidget import BsWidget
> <more imports here>
> 
> class ClassX(BsWidget):
>      <more code here>
> 
> The circular import is as follows:
> 
>   * I want to create a Desktop instance
>   * I try to import shopzeus.yaaf.ui.bootstrap.desktop
>   * That tries to import BsWidget
>   * That tries to import Row
>   * That tries to import BsWidget, which is importing -> I get an
>     "ImportError: cannot import name BsWidger"

Hm, is that cut-and-paste? If so fix the name. If that doesn't work use 
qualified names:

from shopzeus.yaaf.ui.bootstrap import row

[...]
     def AppendRow(self):
         return row.Row(self)

If that still doesn't work follow Ben's advice and be enlightened...