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


Groups > comp.lang.python > #103435

Re: How to define what a class is ?

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: How to define what a class is ?
Date 2016-02-24 20:11 +1100
Message-ID <mailman.86.1456305124.20994.python-list@python.org> (permalink)
References <56cd64fb$0$9220$426a74cc@news.free.fr>

Show all headers | View raw


"ast" <nomail@invalid.com> writes:

> Since a class is an object, I ask myself how to define rigorously what
> a class is.

A class is a type. This bears stating, because for a large part of
Python's history, the two were distinct.

A lot of Python documentation that has its roots in that history still
is careful to maintain the distinction, which in current Python is
completely obsolete.

Every type (and therefore every class) is a template for instances. The
type defines what values are possible for those instances, and also
defines what behaviour those instances will have in common.

> classes are instances from type, but not all, since a class may be an
> instance of a metaclass

Yes. Every class (every type) is an instance of a metaclass, and there
is a particular metaclass named ‘type’.

> A class is always callable

By default, calling a class creates an instance of that class, and
returns that instance to the caller.

> A class inherit from some others classes, so they have a bases
> attribute

That's not part of the definition, really. You need to know that, but
it's not necessary to say what a class is.

> any thing else ?

See the reference documentation on this topic
<URL:https://docs.python.org/3/reference/datamodel.html#objects-values-and-types>.

> Suppose I provide to you an object and that I ask to you to tell me if
> it is a class or not. How would you proceed ?

If the object is an instance of the ‘type’ metaclass, the object is a
type (i.e. a class).

Metaclasses are callable, and return a type (a class) to the caller.

The ‘type’ metaclass, if called with an object as a parameter, will
return the type of that object.

If you have the name ‘foo’ bound to an object, you can call::

    type(foo)

and the metaclass ‘type’ will return the instance of that object's type.
For example::

    >>> type("spam")
    <class 'str'>
    >>> type(None)
    <class 'NoneType'>
    >>> type(3)
    <class 'int'>
    >>> type(int)
    <class 'type'>
    >>> type(type)
    <class 'type'>

If you want a boolean test::

    >>> isinstance(3, type)
    False
    >>> isinstance("spam", type)
    False
    >>> isinstance(int, type)
    True
    >>> isinstance(type, type)
    True

-- 
 \        “The fact of your own existence is the most astonishing fact |
  `\    you'll ever have to confront. Don't dare ever see your life as |
_o__)    boring, monotonous, or joyless.” —Richard Dawkins, 2010-03-10 |
Ben Finney

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


Thread

How to define what a class is ? "ast" <nomail@invalid.com> - 2016-02-24 09:08 +0100
  Re: How to define what a class is ? Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-24 01:46 -0700
    Re: How to define what a class is ? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-25 09:44 +1300
    Re: How to define what a class is ? "ast" <nomail@invalid.com> - 2016-02-25 10:54 +0100
      Re: How to define what a class is ? eryk sun <eryksun@gmail.com> - 2016-02-25 05:21 -0600
  Re: How to define what a class is ? Ben Finney <ben+python@benfinney.id.au> - 2016-02-24 20:11 +1100

csiph-web