Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108586

Re: Distinction between “class” and “type”

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject Re: Distinction between “class” and “type”
Date 2016-05-12 23:42 -0700
Organization A noiseless patient Spider
Message-ID <87shxm4fxh.fsf@jester.gateway.pace.com> (permalink)
References <85eg96eebr.fsf@benfinney.id.au> <mailman.623.1463116040.32212.python-list@python.org>

Show all headers | View raw


Ben Finney <ben+python@benfinney.id.au> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Distinction between “class” and “type” Ben Finney <ben+python@benfinney.id.au> - 2016-05-13 15:07 +1000
  Re: Distinction between “class” and “type” Rustom Mody <rustompmody@gmail.com> - 2016-05-12 22:21 -0700
  Re: Distinction between “class” and “type” Paul Rubin <no.email@nospam.invalid> - 2016-05-12 23:42 -0700
    Re: Distinction between “class” and   “type” Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-05-13 20:24 +1200
  Re: Distinction between “class” and “type” Steven D'Aprano <steve@pearwood.info> - 2016-05-14 15:05 +1000

csiph-web