0

考虑这段代码:

class Page(object):
  def __init__(self, name, title):
    self.name   = name
    self.title  = title
    self.selected = False
  def select(self):                 <-- How can I make this method work?
    for Page in Pages:
        Page.selected = False
    self.selected = True
class Website(object):
  def __init__(self):
    self.index    = Page("index", "Home")
    self.settings = Page("settings", "Settings")
    self.users    = Page("users", "Users")
    self.logs     = Page("logs", "Logs")
    self.faq      = Page("faq", "FAQ")
  def __iter__(self):
    return iter([self.index, self.settings, self.users, self.logs, self.faq])
Pages = Website()

我正在尝试做的事情似乎是非法的。不过,我确信有办法做到这一点。看来我可能不得不在某处重写get方法。非常感谢您的帮助!

这是我打算使用 Bottlepy 使用这些类的方式:

设置页面:

@route('/')
@route('/<selectedPage>')
@route('/<selectedPage>/')
def dynamic_routing(selectedPage='index'):
  for Page in Pages:
     if selectedPage == Page.name:
            Page.select()
  return template('default')

检索页面信息(在 Bottlepy 模板内):

%for Page in Pages:
    %if Page.selected:
        <title>{{Page.title}}</title>                
    %else:
        <title>Page Not Found</title>
    %end
%end

我现在将代码编辑为工作版本。谢谢大家这么快的输入!!!你们真棒!仍然可能不是最好的方法,但我现在想不出另一种方法来解决它。

4

4 回答 4

0

store a ref to the pageset and then loop through the pages in that set

class Page(object):
  def __init__(self, name, title, pageset):
    self.name   = name
    self.title  = title
    self.pageset = pageset
  def select(self):
    for page in self.pageset.pages:
        page.select = False
    self.select = True

class Pageset(object):
  def __init__(self):
    self.index    = Page("index", "Home", self )
    self.settings    = Page("settings", "Settings", self )
    self.pages = [ self.index , self.settings , ]

Note that this gives you the ability to have multiple Pagesets , and you're looking at the class instances. Your code above is referencing the classes themselves ( which there's really no reason to do ). i put self.pages as an attribute, because you might want to store other data in a pageset .

于 2013-03-05T22:44:52.010 回答
0

You need to rename the .select attribute or the .select() method, and add a self parameter to the method, but otherwise your code will work:

class Page(object):
    def __init__(self, name, title):
        self.name   = name
        self.title  = title
        self.selected = False

    def select(self):
        for Page in Pages:
            Page.selected = False
        self.selected = True

With those changes, your code works:

>>> Pages = Pages()
>>> Pages.index
<__main__.Page object at 0x10a0b6cd0>
>>> Pages.index.selected
False
>>> Pages.index.select()
>>> Pages.index.selected
True
>>> Pages.faq.select()
>>> Pages.index.selected
False

However, that is not to say that this is a good architecture design. I'd say you need to move the responsibility to select a page to the Pages class instead, an most of all avoid replacing the class Pages with a global instance Pages.

于 2013-03-05T22:45:15.567 回答
0

我认为这里最好的解决方案是将您的select()方法移至Pages类:

class Pages(object):
    ...
    def select(self, target):
        for page in self:
           page.select = False
        target.select = True

对我来说,这似乎是一个更合乎逻辑的位置,这意味着Page实例不需要知道Pages它们所属的实例。

selected在保存所选页面的实例上简单地具有一个属性甚至可能更好Pages,而不是每个人都Page知道它是否被选中,但这取决于设计。

于 2013-03-05T22:47:04.367 回答
0

Pages 对象管理多个单独的 Page 对象。它应该负责将它们全部作为一个集合涉及的事情,例如,在您的示例中,将单个页面标记为选中并取消标记其他页面。

虽然您可能使每个 Page 都知道拥有它的 Pages 对象,但这样做过多可能会很快导致混乱的意大利面条代码。

PS - 尝试想出一些更好的名字,甚至谈论拥有Page -s 和Pages是不舒服的。

于 2013-03-05T22:49:23.440 回答