Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62158
| From | dieter <dieter@handshake.de> |
|---|---|
| Subject | Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ |
| Date | 2013-12-17 08:14 +0100 |
| References | <mailman.4162.1387158693.18130.python-list@python.org> <52af7bfe$0$29976$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4265.1387264469.18130.python-list@python.org> (permalink) |
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes: > On Mon, 16 Dec 2013 12:51:21 +1100, Ben Finney wrote: > ... "type(obj)" versus "obj.__class__" > That is an excellent question, I only wish I had an excellent answer to > give you. Obviously great minds think alike because I was going to ask > the same question, prompted by this comment from Nick Coghlan on the > python-dev list: > > "...type(obj).__name__ (working with the concrete type, ignoring any > proxying) or obj.__class__.__name__ (which takes proxying into > account)..." > > So there is a difference between them, but I'm not entirely sure what it > is. I understand the difference: sometimes you work with proxies (e.g. "weakref" proxies). A proxie should work mostly like the proxied object - but in rare cases, you want to detect that what you have is actually a proxie rather than the real object. You can use "type(obj)" to check the real type ob "obj" (in some sense, it is more direct - more reliable; giving you the real type of "obj"). "obj.__class__" on the other hand uses standard attribute access - and proxying may have customized attribute access to access the proxied object's attributes rather than its own: then "obj.__class__" would give you not the type of "obj" (the proxie) but that of the proxied object.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Ben Finney <ben+python@benfinney.id.au> - 2013-12-16 12:51 +1100
Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-16 22:17 +0000
Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ dieter <dieter@handshake.de> - 2013-12-17 08:14 +0100
Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Steven D'Aprano <steve@pearwood.info> - 2013-12-17 07:42 +0000
Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-17 23:35 +1300
Re: Type of an object: Ethan Furman <ethan@stoneleaf.us> - 2013-12-17 06:50 -0800
Re: Type of an object: Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-17 15:08 +0000
Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-18 11:15 +1300
Re: Type of an object: Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-17 23:51 +0000
Re: Type of an object: Ethan Furman <ethan@stoneleaf.us> - 2013-12-17 17:10 -0800
Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-19 00:45 +1300
Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-19 00:39 +1300
csiph-web