473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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__.oldDesc 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 1086

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

Similar topics

9
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...
0
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...
4
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
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...
2
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,...
9
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....
5
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
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...
0
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...

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.