473,703 Members | 3,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__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.__getattribut e__('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.__getattribut e__('name') <==> x.name
--->8---

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

---8<---
x.__class__.__c lass__ <type 'type'> x.__class__.__g etattribute__(' __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 2522
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__.__c lass__
<type 'type'>
x.__class__.__g etattribute__(' __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.__getattrib ute__('aaa', 'replace')('a', 'z')
Out[25]: 'zzz'

or even :

In [26]: object.__getatt ribute__('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.__getattribut e__('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***********@a cm.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.__getattribut e__('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__.__c lass__. 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.__geta ttribute__('att rname')
translates to
klass.__getattr ibute__(instanc e, '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__.__g etattribute__(x , '__class__')

<type 'int'>

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

---8<---
x.__class__.__c lass__ <type 'type'> x.__class__.__g etattribute__(' __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__.jo in(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.__getattribut e__('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
2503
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__ defined
6
3634
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 purpose. This works fine if I already have an instantiation of the class, but not when I try this on the class object directly. A bare bones example:
3
2572
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
2060
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
3313
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 custom metaclass with a __getattribute__ method, the method is used when acessing some attribute directly with the class object, but not when you do it from the instance.
1
5725
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 import wx class DefaultValueHolder(object): """Intended for use with wxConfig (or maybe _winreg) to set up and/or get registry key names and values. Name attrs as default*. "default" will be stripped of when reading and...
6
3552
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: class X( object ): y = 'private value' def get_y( self ): return self.y
8
1770
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 works, but would like to take any suggestions you guys have. I've commented out the "delattr" call because it throws an AttributeError (although I don't know why). class LateInitMixIn(object):
14
4348
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 ignored when called. I can see the attribute using dir(self.__class__) on an instance, but when called, python enters __getattr__. If I correct the bug, the attribute calls work as expected and do not call __getattr__. I can't seem to make a simple...
0
8749
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8662
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9243
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7853
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.