473,387 Members | 1,621 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,387 software developers and data experts.

Descriptors: why __get__(obj,typ=None) instead of __get__(obj,typ)

Almost everwhere the descriptor protocol is mentioned, it specifies
__get__(obj, typ=None). Why is a default value needed for the second
argument? In which case does Python call a descriptor without a second
argument?

Thanks,
Shalabh
Jul 18 '05 #1
2 1722
Shalabh Chaturvedi <sh*****@cafepy.com> writes:
Almost everwhere the descriptor protocol is mentioned, it specifies
__get__(obj, typ=None).
Really? I thought it was "__get__(obj, cls=None)" <wink>.
Why is a default value needed for the second argument? In which case
does Python call a descriptor without a second argument?


It doesn't *look* like it does, ever.

/However/ the wrapper for tp_descr_get (typeobject.c:wrap_descr_get)
accepts 1 or 2 arguments translating an absent or None second argument
to NULL, so if you want to behave like C-implemented descriptors,
you'd better accept 1 or 2 arguments. IOW, it's just part of the
"descriptor protocol".

As to *why* it's like this... erm, not sure about that.

Cheers,
mwh

--
Our Constitution never promised us a good or efficient government,
just a representative one. And that's what we got.
-- http://www.advogato.org/person/mrorg...html?start=109
Jul 18 '05 #2
Michael Hudson wrote:
Shalabh Chaturvedi <sh*****@cafepy.com> writes:

....
Why is a default value needed for the second argument? In which case
does Python call a descriptor without a second argument?


It doesn't *look* like it does, ever.

/However/ the wrapper for tp_descr_get (typeobject.c:wrap_descr_get)
accepts 1 or 2 arguments translating an absent or None second argument
to NULL, so if you want to behave like C-implemented descriptors,
you'd better accept 1 or 2 arguments. IOW, it's just part of the
"descriptor protocol".

As to *why* it's like this... erm, not sure about that.

Possibly so that the interface is easier to use directly:

for prop in obj.getProperties():
try:
prop.__get__( obj )
except Exception:
argh()

Most of the time the class argument is unused (for simple property-like
code, anyway), so why not let people omit it when calling directly.
Seems like a good design choice to me.

Enjoy,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/

Jul 18 '05 #3

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

Similar topics

2
by: Denis S. Otkidach | last post by:
I've noticed that the order of attribute lookup is inconsistent when descriptor is used. property instance takes precedence of instance attributes: >>> class A(object): .... def...
8
by: David S. | last post by:
I am looking for a way to implement the same simple validation on many instance attributes and I thought descriptors (http://users.rcn.com/python/download/Descriptor.htm) looked like the right...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
7
by: Simon Bunker | last post by:
Hi I have code similar to this: class Input(object): def __init__(self, val): self.value = val def __get__(self, obj, objtype): return self.value
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
4
by: 7stud | last post by:
Hi, Can someone show me how to manually implement staticmethod()? Here is my latest attempt: ---------------- def smethod(func): def newFunc(): pass
5
by: Bob B. | last post by:
I've been playing with descriptors lately. I'm having one problem I can't seem to find the answer to. I want to assign a descriptor to a list of attributes. I know I should be able to add these...
7
by: mrkafk | last post by:
Hello everyone, I'm trying to do seemingly trivial thing with descriptors: have another attribute updated on dot access in object defined using descriptors. For example, let's take a simple...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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
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,...
0
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...

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.