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


Groups > comp.lang.python > #12282

Re: typing question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
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; 'instance': 0.05; 'python': 0.08; '(rather': 0.09; '>>>>': 0.09; '__name__': 0.09; 'deprecated': 0.09; 'foo': 0.09; 'received:209.85.160.174': 0.09; 'received:mail-gy0-f174.google.com': 0.09; 'am,': 0.12; 'class,': 0.15; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '2.7.2': 0.16; '[gcc': 0.16; 'answer,': 0.16; 'cc:name:python list': 0.16; 'old-style': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'subject:question': 0.18; 'cheers,': 0.18; '>>>': 0.18; 'jason': 0.21; "doesn't": 0.22; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'indirectly': 0.23; 'aug': 0.24; 'classes': 0.28; 'sat,': 0.28; 'pass': 0.29; '27,': 0.29; 'everyone,': 0.29; 'message-id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; '(since': 0.30; 'differently': 0.30; 'semantics': 0.30; '(including': 0.30; "skip:' 10": 0.30; 'class': 0.30; 'chris': 0.32; 'minor': 0.32; 'probably': 0.33; "can't": 0.33; 'there': 0.33; 'difference': 0.34; '...': 0.34; 'quite': 0.34; 'explicit': 0.34; 'received:209.85.160': 0.35; 'object': 0.35; 'explain': 0.36; 'url:python': 0.36; 'question': 0.36; 'skip:" 10': 0.36; 'class.': 0.37; 'but': 0.37; 'two': 0.37; 'some': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'url:docs': 0.39; 'basic': 0.39; 'why': 0.39; 'more': 0.60; 'kinds': 0.70; 'case?': 0.84; 'sender:addr:chris': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; 'jul': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=HGDJ/M4NUML/COel2C6IKOsqJFlKm3tCr65COIHoXzM=; b=C38HnKEOctwA0d6vpBELKp+tEvFFQzFaQaprB/cY+5EhGgp0L1wJ9Hn3RH1eFTGuft YfSBT3lWUTIlWbXwuAEUIzDEyH8tXqKEp1aF4+8Fcbohx3tBdrtKp2hSbQtdBjZdsFdZ yRyD9kjCsY1HD+yJ6GmWjyOn5HTdZeqOfFfDs=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <CAEk9e3qF7LRnaFOU7bZSC5COnTr0v9Ch1B5pKT_9zTE1=12Y4w@mail.gmail.com>
References <CAEk9e3qF7LRnaFOU7bZSC5COnTr0v9Ch1B5pKT_9zTE1=12Y4w@mail.gmail.com>
Date Sat, 27 Aug 2011 10:35:14 -0700
X-Google-Sender-Auth ziTPPn4tEgzSqNjB3peGE5nebhs
Subject Re: typing question
From Chris Rebert <clp2@rebertia.com>
To Jason Swails <jason.swails@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.471.1314466517.27778.python-list@python.org> (permalink)
Lines 77
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314466517 news.xs4all.nl 2431 [2001:888:2000:d::a6]:48471
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12282

Show key headers only | View raw


On Sat, Aug 27, 2011 at 6:42 AM, Jason Swails <jason.swails@gmail.com> wrote:
> Hello everyone,
>
> This is probably a basic question with an obvious answer, but I don't quite
> get why the type(foo).__name__ works differently for some class instances
> and not for others.  If I have an "underived" class, any instance of that
> class is simply of type "instance".  If I include an explicit base class,
> then its type __name__ is the name of the class.
>
> $ python
> Python 2.7.2 (default, Aug 26 2011, 22:35:24)
> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> class MyClass:
> ...     pass
> ...
>>>> foo = MyClass()
>>>> type(foo)
> <type 'instance'>
>>>> type(foo).__name__
> 'instance'
>>>> class MyClass1():
> ...     pass
> ...
>>>> bar = MyClass1()
>>>> type(bar)
> <type 'instance'>
>>>> type(bar).__name__
> 'instance'
>>>> class MyClass2(object):
> ...     pass
> ...
>>>> foobar = MyClass2()
>>>> type(foobar)
> <class '__main__.MyClass2'>
>>>> type(foobar).__name__
> 'MyClass2'
>
> I can't explain this behavior (since doesn't every class inherit from object
> by default?

That's only true in Python 3.x.

Python 2.7.2 (default, Jul 27 2011, 04:14:23)
>>> class Foo:
...     pass
...
>>> Foo.__bases__
()
>>> class Bar(object):
...     pass
...
>>> Bar.__bases__
(<type 'object'>,)

> And if so, there should be no difference between any of my class
> definitions).  I would prefer that every approach give me the name of the
> class (rather than the first 2 just return 'instance').  Why is this not the
> case?

Classes directly or indirectly inheriting from `object` are
"new-style"; classes which don't are "old-style". The two kinds of
classes have different semantics (including whether they have a
.__name__, but that's minor relative to the other changes). Old-style
classes are deprecated and were removed in Python 3.
See http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes

Cheers,
Chris

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


Thread

Re: typing question Chris Rebert <clp2@rebertia.com> - 2011-08-27 10:35 -0700

csiph-web