Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

python - Why does ordering matter in type hinting?

Why is this ok

class Ship:

    def __init__(self, parent):
        self.parent = parent

class Fleet:

    def __init__(self):
        self.ships = []

    def add_ship(self, ship: Ship):
        self.ships.append(ship)

But this is not?

class Fleet:

    def __init__(self):
        self.ships = []

    def add_ship(self, ship: Ship):
        self.ships.append(ship)

class Ship:

    def __init__(self, parent):
        self.parent = parent

I know that you cannot have circular references in importing. However, this is not an import thing: both of these are in the same file. In both cases the definition of Ship is made, but it seems as though if Fleet is defined first it can't find the definition of Ship. This is not true if I used isinstance to check the type.

def add_ship(self, ship):
    if isinstance(ship, Ship): # works fine
        self.ships.append(ship)

However, that does not allow my IDE (PyCharm) to see the definition and autocomplete syntax.

In fact, the following design pattern seems to work fine

class Fleet:

    def __init__(self):
        self.ships = []

    def add_ship(self, ship):
        if isinstance(ship, Ship):
            self.ships.append(ship)

class Ship:

    def __init__(self, parent):
        if isinstance(parent, Fleet):
            self.parent = parent

But, again, does not allow for my IDE to figure out the types. This is Python 3.6.5/Anaconda/Windows 10.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

def add_ship(self, ship: Ship): is evaluated at definition time. At that time Ship is not defined yet.

if isinstance(ship, Ship): is evaluated only when add_ship is called. This will (hopefully) be only after Ship had been defined.

PEP 563 (implemented in Python 3.7 and above) solves this by making the type annotations evaluation lazy.

If you upgrade, you can add from __future__ import annotations to the top of the file and that example will work.

This will be fully implemented in Python 4.0, meaning from __future__ import annotations will not be needed.

Pre Python 3.7 solution

If you can't or don't want to upgrade to Python >= 3.7 you can use a string-literal annotation (which is also mentioned in the PEP I linked to):

def add_ship(self, ship: 'Ship'):

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...