用於區分類型別名與實際類別的約定?

[英]Convention to distinguish type aliases from actual classes?


The typing module allows assigning complex type signatures to aliases, which can then be used interchangeably with the actual type. This seems to have the problem that it confuses type aliases with actual classes, i.e. in a definition like this

輸入模塊允許將復雜類型簽名分配給別名,然后可以與實際類型互換使用。這似乎有一個問題,即它將類型別名與實際類混淆,即在這樣的定義中

def foo(obj: MyComplexObject):
    ...

there is no way to tell if MyComplexObject is implemented as a class somewhere or is just a type alias. This seems to be an unfortunate source of confusion to me, especially when the alias is (re)used far from its original definition. Is that generally accepted or is there a convention to somehow distinguish type aliases from actual classes?

沒有辦法判斷MyComplexObject是作為某個類實現的,還是只是一個類型別名。這對我來說似乎是一個令人遺憾的混淆源,特別是當別名(重新)遠離其原始定義時。這是普遍接受的還是有某種慣例以某種方式區分類型別名和實際類?

2 个解决方案

#1


2  

I believe type aliases and regular types are indistinguishable by design: type aliases are meant to be exact stand-ins for whatever type they're aliasing.

我認為類型別名和常規類型在設計上是無法區分的:類型別名對於它們是別名的任何類型都是准確的替身。

So, to use your example, it doesn't necessarily matter if MyComplexObject is an actual class or a type alias: they both behave equivalently at runtime and static typecheck time.

因此,要使用您的示例,MyComplexObject是實際類還是類型別名並不一定重要:它們在運行時和靜態類型檢查時都表現相同。

And because the type checker can give you immediate feedback on whether or not you're using obj in a type-safe way, there are minimal opportunities to get confused: either you'll use the obj parameter correctly or you'll get immediate feedback otherwise. (And similarly, IDEs such as Pycharm that understand PEP 484 semantics will also understand type aliases and will correctly auto-complete/flag mistakes.)

並且因為類型檢查器可以為您提供關於您是否以類型安全方式使用obj的即時反饋,所以很少有機會混淆:要么您將正確使用obj參數,要么您將獲得即時反饋除此以外。 (同樣地,理解PEP 484語義的諸如Pycharm之類的IDE也將理解類型別名並且將正確地自動完成/標記錯誤。)

It's also worth noting that in the case where you specifically alias other classes, the alias and the original class are literally indistinguishable at runtime: they're both variables that refer to the same underlying type object. (So arguably, there's nothing to actually be confused about: both the alias and the original type are the "same thing".)

值得注意的是,在您特別為其他類別名的情況下,別名和原始類在運行時幾乎無法區分:它們都是引用相同底層類型對象的變量。 (可以說,實際上沒有什么可以混淆的:別名和原始類型都是“相同的東西”。)

Consequently, there's been very little incentive to find a naming convention for distinguishing between types and type aliases -- it doesn't really end up being an issue in practice.

因此,很難找到一種區分類型和類型別名的命名約定 - 它實際上並不是真正的問題。


That said, there are some implicit conventions for when to use type aliases. For example, people usually don't alias classes directly: type aliases, as Ethan said, are mainly meant to be used to help simplify larger compound types that are repeated in multiple places.

也就是說,何時使用類型別名有一些隱含的約定。例如,人們通常不直接使用別名:類型別名,如Ethan所說,主要用於幫助簡化在多個地方重復的較大化合物類型。

This means that type aliases are mainly used when you would otherwise have a bunch of really ugly type signatures. It then becomes a "tradeoff" sort of thing: the reader does need to spend time looking up the definition of an alias and cache it in their head, but once they do, reading the rest of the code ought to be much more pleasant.

這意味着類型別名主要用於否則會有一堆非常難看的類型簽名。然后它成為一種“權衡”的東西:讀者確實需要花時間查找別名的定義並將其緩存在他們的頭腦中,但是一旦他們這樣做,閱讀其余的代碼應該更加愉快。

So if you still find type aliases potentially confusing and prefer to avoid using them unless necessary, that's perfectly fine: that's what pretty much everybody else does anyways. The only difference comes down to when you cross that tipping point from "unnecessary" to "necessary".

因此,如果您仍然發現類型別名可能會造成混淆,並且除非必要,我們更願意避免使用它們,這非常好:這就是其他所有人都做的事情。唯一的區別在於你從“不必要”到“必要”的那個轉折點。

#2


1  

There is no official convention for distinguishing the two.

沒有官方公約來區分這兩者。

Type aliases are very similar to typedefs in other languages. However, by convention type aliases are used for complex combinations of types, such as Message = Union[str, MyClass1, MyClass2, OtherClass], where writing out the full union multiple times would be cumbersome. So a single name annotation should either be: a type, or some combination of types that are aliased. You must go to the definition of that name to tell them apart.

類型別名與其他語言中的typedef非常相似。但是,按慣例,類型別名用於復雜的類型組合,例如Message = Union [str,MyClass1,MyClass2,OtherClass],其中多次寫出完整聯合將是麻煩的。因此,單個名稱注釋應該是:類型或別名的某些類型組合。您必須轉到該名稱的定義以區分它們。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/05/06/a82bbf0d76fe87d17bd7f90ad0fcd710.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com