Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.python Subject: Re: Distinction between =?utf-8?B?4oCcY2xhc3PigJ0=?= and =?utf-8?B?4oCcdHlwZeKAnQ==?= Date: Thu, 12 May 2016 23:42:34 -0700 Organization: A noiseless patient Spider Lines: 42 Message-ID: <87shxm4fxh.fsf@jester.gateway.pace.com> References: <85eg96eebr.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="52eac348160ce62b68e4635eee7cc854"; logging-data="4624"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KRlBturY1Eh72zQldzzwa" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:g1g2e30h0hCPx7CLdQXqvOuQCdw= sha1:u9x3X/ZQiE4u3LzPAjd/fe0Mua4= Xref: csiph.com comp.lang.python:108586 Ben Finney writes: > There's a big overlap because most classes are also types -- but not > the other way around! E.g. Any is a type but not a class (you can > neither inherit from Any nor instantiate it), and the same is true > for unions and type variables. […] > As a Bear of Little Brain, this leaves me clueless. What is the > distinction Guido alludes to, and how are Python classes not also types? I thought I understood Guido's explanation but maybe I missed something. Let C be a class, maybe defined by a class statement or maybe a builtin like "int". You can make an instance of C the usual way: x = C() And you can have a type annotation that says function f expects an arg that is an instance of C: def f(x : C) -> int: ... You might alternatively write a function whose arg must be either an int or a string: def f(s : Union[int, str]) -> int : ... or (I think, I haven't tried it) you can equivalently bind that type to a variable: T = Union[int, str] def f(s : T) -> int : ... The point here is that T is a type but it is not a class. You can't instantiate T by saying x = T() and expecting to get back some value that is (indeterminately) an int or a string. That is, there's stuff (like instantiation) that you can do with types that happen to be classes, but there are also types that aren't classes.