如何获取我编写的生成器表达式的所有元素?

发布于 2024-08-31 16:23:25 字数 1140 浏览 9 评论 0原文

我正在使用 google-app-engine webapp,部分代码是:

class Post(db.Model):
    title = db.StringProperty(required=True)
    def categories(self):
        return (x.category for x in self.postcategory_set)

class Category(db.Model):
    name = db.StringProperty()

class PostCategory(db.Model):
    post = db.ReferenceProperty(Post)
    category = db.ReferenceProperty(Category)


class sss(webapp.RequestHandler):
  def get(self):
    obj1 = Post(title='hhaa')
    #obj1.title = 'haha'
    obj1.put()

    obj2 = Category()

    obj2.name='haha-kao'

    obj2.put()
    obj3=PostCategory()
    obj3.post=obj1
    obj3.category=obj2
    obj3.put()
    self.response.out.write(obj1.categories().get().name)

但我收到错误:

Traceback (most recent call last):
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 511, in __call__
    handler.get(*groups)
  File "D:\zjm_code\helloworld\a.py", line 131, in get
    self.response.out.write(obj1.categories().get().name)
AttributeError: 'generator' object has no attribute 'get'

那么如何获取我编写的 genexp 的所有值?

I am using google-app-engine webapp, part of the code is :

class Post(db.Model):
    title = db.StringProperty(required=True)
    def categories(self):
        return (x.category for x in self.postcategory_set)

class Category(db.Model):
    name = db.StringProperty()

class PostCategory(db.Model):
    post = db.ReferenceProperty(Post)
    category = db.ReferenceProperty(Category)


class sss(webapp.RequestHandler):
  def get(self):
    obj1 = Post(title='hhaa')
    #obj1.title = 'haha'
    obj1.put()

    obj2 = Category()

    obj2.name='haha-kao'

    obj2.put()
    obj3=PostCategory()
    obj3.post=obj1
    obj3.category=obj2
    obj3.put()
    self.response.out.write(obj1.categories().get().name)

but I get an error:

Traceback (most recent call last):
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 511, in __call__
    handler.get(*groups)
  File "D:\zjm_code\helloworld\a.py", line 131, in get
    self.response.out.write(obj1.categories().get().name)
AttributeError: 'generator' object has no attribute 'get'

so how can I obtain all values of the genexp I wrote?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-09-07 16:23:25

如果您想要一个 all 方法,您应该返回一个列表理解而不是生成器:

return [x.category for x in self.postcategory_set]

但由于 postcategory_set 已经是一个可迭代的(因为您迭代了它)

return self.postcategory_set

会更自然,但是这将为调用者提供对您的私有可变数据的引用,但在本例中 Post.postcategory_set并不是真正的 set 类型,而是 google.app.engine.ext.db.Query 确实是可迭代的,但不是可变的。

你没有问,但你的模型很有趣,当你可以:

class Post(db.Model):
    title = db.StringProperty(required=True)
    category = db.ReferenceProperty(Category)
    def categories(self):
        return list(self.postcategory_set)

并完全删除 PostCategory 时,PostCategory 表是不必要的。

If you wanted an all method you should return a list comprehension instead of a generator:

return [x.category for x in self.postcategory_set]

but since postcategory_set is already an iterable (because you iterated it)

return self.postcategory_set

would be more natural, but that would give the caller a reference to your private, mutable data, except in this case Post.postcategory_set is not really a set type but rather an instance of google.app.engine.ext.db.Query which is indeed iterable but not mutable.

And you didn't ask, but your model is funny, with the PostCategory table being unnecessary when you could have:

class Post(db.Model):
    title = db.StringProperty(required=True)
    category = db.ReferenceProperty(Category)
    def categories(self):
        return list(self.postcategory_set)

And drop PostCategory completely.

无法回应 2024-09-07 16:23:25

您需要的是 categories().next() 。生成器没有 get

It is categories().next() that you need. Generators have no get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文