Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: How to define what a class is ? Date: Wed, 24 Feb 2016 20:11:36 +1100 Lines: 84 Message-ID: References: <56cd64fb$0$9220$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de QnwxJguV+CbL17q6zk8mYQUp07Yj7CGdjQnfVhEAxaFQ== Cancel-Lock: sha1:rUlgFZjO5DuqJb7Lh7AIZ2aMfT0= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes,': 0.05; 'defines': 0.07; 'type,': 0.07; 'subject:How': 0.09; 'caller.': 0.09; 'definition,': 0.09; 'instances.': 0.09; "object's": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type)': 0.09; 'python': 0.10; 'template': 0.11; 'boolean': 0.16; 'boring,': 0.16; 'callable,': 0.16; 'class)': 0.16; 'class).': 0.16; 'instances,': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'parameter,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:class': 0.16; 'attribute': 0.18; 'creates': 0.18; '>>>': 0.20; 'all,': 0.20; 'class,': 0.22; 'default,': 0.22; 'object.': 0.22; 'suppose': 0.22; "python's": 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'define': 0.27; 'not.': 0.27; 'subject: ?': 0.27; 'object,': 0.27; 'values': 0.28; 'behaviour': 0.29; 'callable': 0.29; 'subject:what': 0.29; 'classes': 0.30; 'topic': 0.32; 'class': 0.33; 'url:python': 0.33; 'instances': 0.33; 'that,': 0.34; 'false': 0.35; 'instance': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'possible': 0.36; '(and': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'say': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'some': 0.40; 'url:3': 0.60; 'ever': 0.60; 'your': 0.60; "you'll": 0.61; 'provide': 0.61; 'bases': 0.63; 'is.': 0.63; 'inherit': 0.66; 'life': 0.67; 'therefore': 0.67; '_o__)': 0.84; 'history,': 0.84; 'monotonous,': 0.84; 'received:125': 0.84; 'url:datamodel': 0.84; 'bears': 0.91; 'careful': 0.91; 'dare': 0.91; 'url:reference': 0.91; '\xe2\x80\x9cthe': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103435 "ast" 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 . > 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") >>> type(None) >>> type(3) >>> type(int) >>> type(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