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


Groups > comp.lang.python > #103492

Re: How to define what a class is ?

From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to define what a class is ?
Date 2016-02-25 05:21 -0600
Message-ID <mailman.121.1456399345.20994.python-list@python.org> (permalink)
References <56cd64fb$0$9220$426a74cc@news.free.fr> <mailman.85.1456303651.20994.python-list@python.org> <56cecf47$0$3305$426a74cc@news.free.fr>

Show all headers | View raw


On Thu, Feb 25, 2016 at 3:54 AM, ast <nomail@invalid.com> wrote:
> So we can conclude that inspect.isclass(x) is equivalent
> to isinstance(x, type)
>
> lets have a look at the source code of isclass:
>
> def isclass(object):
>    """Return true if the object is a class.
>
>    Class objects provide these attributes:
>        __doc__         documentation string
>        __module__      name of module in which this class was defined"""
>    return isinstance(object, type)

Except Python 2 old-style classes (i.e. 2.x classes that aren't a
subclass of `object`) are not instances of `type`. Prior to new-style
classes, only built-in types were instances of `type`. An old-style
class is an instance of "classobj", and its instances have the
"instance" type.

    >>> class A: pass
    ...
    >>> type(A)
    <type 'classobj'>
    >>> type(A())
    <type 'instance'>

Note that "classobj" and "instance" are instances of `type`.

The `isclass` check in Python 2 has to instead check
isinstance(object, (type, types.ClassType)).

    >>> types.ClassType
    <type 'classobj'>

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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