0

I have some codes like this:

@zhihu.log_attr
@zhihu.iter_factory('voters')
def voters(x):
    from .People import People
    return People(x)

But the IDE ... Imgur Image

So I try this way:

@zhihu.log_attr
@zhihu.iter_factory('voters')
def voters(x)->People:
    from .People import People
    return People(x)

But it don't works.

NameError: name 'People' is not defined
ChezzOne
  • 31
  • 2
  • Just as a side note, Sublime isn't an IDE; it's a text editor. It uses an external Python interpreter to run your program, so if you see errors in your code it's unlikely that Sublime has anything to do with it (unless there are special steps to be taken before running your program like activating a virtual environment or something). – OdatNurd Aug 16 '18 at 16:39

1 Answers1

0

If the return type is not defined when you're defining your function you can specify the return type as a string:

@zhihu.log_attr
@zhihu.iter_factory('voters')
def voters(x) -> 'People':
    from .People import People
    return People(x)

PyCharm is smart enough to use the above to look up object attributes, hopefully Sublime is too.

Edit: a more detailed answer here: Name not defined in type annotation

101
  • 8,514
  • 6
  • 43
  • 69
  • Thanks for your help. However, this 'People' class is a subclass of this class and be defined in another file. I have tried your way, but it don't work. Nothing happened – ChezzOne Aug 16 '18 at 05:01
  • It may be that Sublime doesn't recognise the type hint. Have you tried another IDE such as PyCharm? – 101 Aug 16 '18 at 06:03
  • You also probably want to avoid naming both the module and the class exactly the same as it's confusing and unconventional. Typically the module name is lowercase, e.g. `from people import People`. – 101 Aug 16 '18 at 06:08
  • Thanks for your patiention. – ChezzOne Aug 16 '18 at 07:37
  • No worries. Patiention is my new favourite word. – 101 Aug 17 '18 at 00:05
  • Hi, I think I find the question. – ChezzOne Aug 17 '18 at 02:41
  • `@zhihu.iter_factory('voters')` will return a complex iterator, like this: – ChezzOne Aug 17 '18 at 02:43
  • def iter_factory(url_function_name, method=json): def decorate(deal_function): def iter_function(count=-1, start=1, page=-1)->Iterable[People()]: ... – ChezzOne Aug 17 '18 at 02:44