472,779 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

__getattribute__ doesn't work on 'type' type for '__class__'

I'm running this version of Python:

Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin

I read in the documentation that these two expressions are
interchangeable:

x.__getattribute__('name') <==> x.name

From "pydoc __getattribute__":

---8<---
Help on method-wrapper object:

__getattribute__ = class method-wrapper(object)
| Methods defined here:
|
| __call__(...)
| x.__call__(...) <==> x(...)
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
--->8---

Yet when I try this with the 'type' type, it doesn't work:

---8<---
x.__class__.__class__ <type 'type'> x.__class__.__getattribute__('__class__')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor '__getattribute__' requires a 'int' object but
received a 'str'
--->8---

Why is this?

-- Barry

--
http://barrkel.blogspot.com/
Jun 20 '06 #1
5 2450
Le Mardi 20 Juin 2006 18:36, Barry Kelly a écrit*:
Yet when I try this with the 'type' type, it doesn't work:

---8<---
x.__class__.__class__
<type 'type'>
x.__class__.__getattribute__('__class__')

you're looking for getattr :

In [11]: getattr(object, '__class__')
Out[12]: <type 'type'>

In [13]: getattr(str, 'translate')
Out[13]: <method 'translate' of 'str' objects>
Traceback (most recent call last):
* File "<stdin>", line 1, in ?
TypeError: descriptor '__getattribute__' requires a 'int' object but
received a 'str'
--->8---

Why is this?


__getattribute__ is the descriptor interface, that means... humm see the doc
i'm not enough skilled in english to explain in few words...
but in python :

In [27]: 'aaa'.replace('a', 'z')
Out[27]: 'zzz'

is exactly the same as :

In [25]: str.__getattribute__('aaa', 'replace')('a', 'z')
Out[25]: 'zzz'

or even :

In [26]: object.__getattribute__('aaa', 'replace')('a', 'z')
Out[26]: 'zzz'

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Jun 20 '06 #2
Barry Kelly wrote:
I read in the documentation that these two expressions are
interchangeable:
x.__getattribute__('name') <==> x.name


I think you'll find:
getattr(x, 'name') <==> x.name

is what is equivalent. You can define a '__getattribute__'
method which will get used, but I doublbt thre is a promise for
an exposed name '__getattribute__'.

--
--Scott David Daniels
sc***********@acm.org
Jun 20 '06 #3
Barry Kelly a écrit :
I'm running this version of Python:

Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin

I read in the documentation that these two expressions are
interchangeable:

x.__getattribute__('name') <==> x.name
I wouldn't say they are interchangeable. FWIW, __getattribute__ starts
and ends with two underscores which in Python - and a lot of other
languages - has a very strong 'language internals, black magic stuff,
not intented for direct client code use'.

Yet when I try this with the 'type' type, it doesn't work:

---8<---
x.__class__.__class__
<type 'type'>
x.__class__.__getattribute__('__class__')
You're calling __getattribute__ on x.__class__, not on
x.__class__.__class__. But anyway, __getattribute__ is a descriptor,
which implies different behaviour when called on a class object. Please
read the doc on the descriptor protocol. For making long things short,
instance.__getattribute__('attrname')
translates to
klass.__getattribute__(instance, 'attrname')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor '__getattribute__' requires a 'int' object but
received a 'str'


I deduce from this that your 'x' is an int. Since you're calling
__getattribute__ on the type 'int', you have to pass an int as the first
parameter, ie:
x = 1
x.__class__.__getattribute__(x, '__class__')

<type 'int'>

Note BTW that it returns x.__class__, not x.__class__.__class__
Jun 20 '06 #4
Barry Kelly wrote:
[snipped]
Yet when I try this with the 'type' type, it doesn't work:

---8<---
x.__class__.__class__ <type 'type'> x.__class__.__getattribute__('__class__') Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor '__getattribute__' requires a 'int' object but
received a 'str'
--->8---

Why is this?
The problem is that your class (I would guess that x is an int) and its
type have a method with the same name. As is normal for attribute
lookup, the instance's attribute is first looked up in its __dict__.
Since x.__class__ is a type, this results in __getattribute__
being an unbound method of that type. What you are doing
is similar to:
L = ["spam", "eggs"]
"".__class__.join(L)

Traceback (most recent call last):
...
TypeError: descriptor 'join' requires a 'str' object but received a
'list'

Which as you can see, fails with the same error message.
-- Barry

--
http://barrkel.blogspot.com/


Hope this helps,

Ziga

Jun 20 '06 #5
Barry Kelly <ba***********@gmail.com> wrote:
From "pydoc __getattribute__":

---8<---
Help on method-wrapper object:

__getattribute__ = class method-wrapper(object)
| Methods defined here:
|
| __call__(...)
| x.__call__(...) <==> x(...)
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
--->8---


Thanks for the answers, folks. I did find getattr() quite quickly from
Google, but it's clear that the documentation with Python is wrong.

-- Barry

--
http://barrkel.blogspot.com/
Jun 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__...
6
by: Ruud de Jong | last post by:
I have the situation where I need to construct the name of a static method, and then retrieve the corresponding function from a class object. I thought I could just use __getattribute__ for this...
3
by: Sylvain Ferriol | last post by:
hello when i define __getattribute__ in a class, it is for the class instances but if i want to have a __getattribute__ for class attributes how can i do that ? sylvain
5
by: Stefan Sonnenberg-Carstens | last post by:
Hi there, I'm facing some strange things - but maybe only me is strange - anyway... i wrote the following code: +++ class T(object): def __init__(self,name='',port=80): self.name=name
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
1
bartonc
by: bartonc | last post by:
Still have to prepend "default" when assigning variables to this class, but it now allows val = inst.vaule:"""Encapuslate a Default Values Object and Config File""" from inspect import getmembers...
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
8
by: bukzor | last post by:
I want to make a MixIn class that waits to initialize its super- classes until an attribute of the object is accessed. Not generally useful, but desirable in my case. I've written this, and it...
14
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.