473,770 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of descriptor __get__: what defines an object as being a class?

I'm talk from the point of view of descriptors. Consider

a.x = lambda self:None # simple function

When a.x is later got, what criterion is used to see if a class (and so the
func would have __get__(None, a) called on it)? Pre-metaclasses, one might
assume it was

isinstance(a, (types.TypeType , types.ClassType ))

but nowadays the type of a type could be anything. Is there a simple
Python-level interface to determine what constitutes a type, as there is
for, say, iterators? Could an object start being a type just by
appropriately setting certain members, or is this distinction enforced at a
lower level?

To confuse matters further, the descriptor docs in 2.4 say that descriptors
can only be bound to objects, not classic class instances, but a few moments
with a Python console disproves this (it is the descriptors themselves that
have to be objects.)
def getting(*a): print "Getting!"
class oldDesc: __get__ = getting
class newDesc(object) : __get__ = getting
class Q:pass # classic class
Q.x = oldDesc()
Q.x <__main__.oldDe sc instance at 0x009ACAA8> Q.y = newDesc()
Q.y

Getting!

This seems to be a more sensible state of affairs (otherwise
function-to-method wrapping would have to be a special case), but
http://www.python.org/doc/2.4/ref/de...nvocation.html says different.

What is the canonical distinction between classes and types, and between
instances and objects? How would it apply in the face of such perversions as

from types import *
class C1: __metaclass__ = TypeType
class C2(object): __metaclass__ = ClassType

or reassignment of __class__ from an class to a type or vice versa? I would
have thought that "most of the time" (i.e. excluding the above tricksiness)
all that would be needed was

isinstance(x, object)

but this returns true when x is an Exception, which is guaranteed to be an
instance and hence not an object.

Thanks

John
Jul 18 '05 #1
0 1101

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

Similar topics

9
1421
by: John Roth | last post by:
Using Python 2.2.3, I create this script: class AnObject(object): "This might be a descriptor, but so far it doesn't seem like it" def __init__(self, somedata): self.it = somedata def __get__(self, obj, type=None): print "in get method"
0
1097
by: Eric Huss | last post by:
I'm trying to write a descriptor (similar to the EiffelDescriptor example in the Demo directory). However, I noticed an odd behavior that descriptors written in pure Python mask the underlying object they are wrapping, whereas the descriptors written in C do not. For example, taking the code from http://users.rcn.com/python/download/Descriptor.htm which implements what one would think is a straightforward implementation: class...
4
1244
by: john | last post by:
Hello, I was wondering if someone could explain the following situation to me please: >>> class C(object): def f(self): pass >>> c = C() >>> c.f
3
1095
by: Gary Stephenson | last post by:
I want to define a class-level attribute that will be shared by all subclasses. That is, I want this class, every subclass of this class, every instance of this class and every instance of every subclass to be able to read and write to the _same_ slot/variable. But I can't figure out how to do it. My attempts so far have led me to using a descriptor as follow, but it doesn't get me where I want to be: class sharedClassVar(object):
2
1544
by: WaterWalk | last post by:
Hello, I was recently learning python decorator and descriptor and emulated a @classmethod decorator: class EmuClassMethod(object): def __init__(self, f=None): self.f = f def __get__(self, obj, klass=None): if klass is None: klass = type(obj) def wrapped(*args): return self.f(klass, *args)
9
1439
by: Raymond Hettinger | last post by:
I had an idea but no time to think it through. Perhaps the under-under name mangling trick can be replaced (in Py3.0) with a suitably designed decorator. Your challenge is to write the decorator. Any trick in the book (metaclasses, descriptors, etc) is fair game. Raymond -------- how we currently localize method access with name mangling
5
1438
by: Erwan Adam | last post by:
Hi all, I have a problem with the use of descriptors and super. The file is : # --------------- class Desc(object): def __init__(self, class_name): self.class_name = class_name
3
2031
by: Eric Mahurin | last post by:
Is there a standard way to get a descriptor object for an arbitrary object attribute - independent of whether it uses the descriptor/ property protocol or not. I want some kind of handle/reference/ pointer to an attribute. I know I could make my own class to do this (using the __dict__ of the object and the attribute name), but I would like to use something standard (at least a protocol) if it exists. All that is needed in the protocol...
3
1575
by: cyril giraudon | last post by:
Hello, I try to use python descriptors to define attributes with default value (the code is reported below). But apparently, it breaks the docstring mechanism. help(Basis) shows the right help but help(Rectangle) shows only two lines : " Help on class Rectangle in module basis2:
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
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...
1
3987
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
3589
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.