473,765 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do descriptors (and thus properties) only work on attributes.

Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.

So what are the reasons for limiting this feature in such a way?

--
Antoon Pardon
Jul 18 '05 #1
14 1643
Antoon Pardon <ap*****@forel. vub.ac.be> writes:
Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.


How would that work?
Jul 18 '05 #2
Op 2005-02-28, Paul Rubin schreef <http>:
Antoon Pardon <ap*****@forel. vub.ac.be> writes:
Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.


How would that work?


Well AFAIU a descriptor is an object with at least one method out of
__get__, __set__ or __del__. I don see why implicitly calling one
of these methods would be any more difficult when they are autonomous
objects than when they are attributes.

--
Antoon Pardon
Jul 18 '05 #3
Antoon Pardon wrote:
Op 2005-02-28, Paul Rubin schreef <http>:
Antoon Pardon <ap*****@forel. vub.ac.be> writes:
Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.


How would that work?


Well AFAIU a descriptor is an object with at least one method out of
__get__, __set__ or __del__. I don see why implicitly calling one
of these methods would be any more difficult when they are autonomous
objects than when they are attributes.


I still don't see how that is supposed to work for "a lot of interesting
things". Can you provide examples for one of these interesting things?

--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
Antoon Pardon wrote:
Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.


How would that work?

Well AFAIU a descriptor is an object with at least one method out of
__get__, __set__ or __del__. I don see why implicitly calling one
of these methods would be any more difficult when they are autonomous
objects than when they are attributes.


I guess properties are really a feature of the class, not of the
attribute. Certain operations on objects of the class (getattr, setattr,
delattr) have to be intercepted. If you want to have this for general
variable access, you 'd have to intercept all accesses to local and
module name spaces. This slows things down a lot. And many think that
overloading assignment is a bad idea. You probably find some dicussions
when searching for "overloadin g assignment" in the newsgroup archive.
to an object or class. I think a lot of interesting things one can


As in the chinese curse "May you live in interisting times"?

Daniel
Jul 18 '05 #5
Op 2005-02-28, Diez B. Roggisch schreef <de*********@we b.de>:
Antoon Pardon wrote:
Op 2005-02-28, Paul Rubin schreef <http>:
Antoon Pardon <ap*****@forel. vub.ac.be> writes:
Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.

How would that work?


Well AFAIU a descriptor is an object with at least one method out of
__get__, __set__ or __del__. I don see why implicitly calling one
of these methods would be any more difficult when they are autonomous
objects than when they are attributes.


I still don't see how that is supposed to work for "a lot of interesting
things". Can you provide examples for one of these interesting things?


Lazy evaluation where the value of something is calculated the first
time it is needed but accessed from some storage if it is needed again.

--
Antoon Pardon
Jul 18 '05 #6
Op 2005-02-28, Daniel Dittmar schreef <da************ @sap.corp>:
Antoon Pardon wrote:
Can anyone explain why descriptors only work when they are an attribute
to an object or class. I think a lot of interesting things one can
do with descriptors would be just as interesting if the object stood
on itself instead of being an attribute to an other object.

How would that work?

Well AFAIU a descriptor is an object with at least one method out of
__get__, __set__ or __del__. I don see why implicitly calling one
of these methods would be any more difficult when they are autonomous
objects than when they are attributes.


I guess properties are really a feature of the class, not of the
attribute. Certain operations on objects of the class (getattr, setattr,
delattr) have to be intercepted. If you want to have this for general
variable access, you 'd have to intercept all accesses to local and
module name spaces. This slows things down a lot.


OK, I can understand this.
And many think that
overloading assignment is a bad idea.
But not bad enough to allow it in some cases?
You probably find some dicussions
when searching for "overloadin g assignment" in the newsgroup archive.


I'll take a look.
to an object or class. I think a lot of interesting things one can


As in the chinese curse "May you live in interisting times"?


Well I think python partly makes the time interesting. So in that
respect we are all cursed.

--
Antoon Pardon
Jul 18 '05 #7
In article <cv**********@n ews.sap-ag.de>,
Daniel Dittmar <da************ @sap.corp> wrote:

As in the chinese curse "May you live in interisting times"?


Not so Chinese:

http://www.noblenet.org/reference/inter.htm
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
Jul 18 '05 #8
Aahz:
Not so Chinese: http://www.noblenet.org/reference/inter.htm


That page is incredible!
Me too I have a gut feeling that the citation is from the XX century
and from
some Western country (even if till now I had not questioned its
attribution).
I wish I could say more about its origin ...

Michele Simionato

Jul 18 '05 #9
On 2005-02-28, Antoon Pardon <ap*****@forel. vub.ac.be> wrote:
Op 2005-02-28, Diez B. Roggisch schreef <de*********@we b.de>:
I still don't see how that is supposed to work for "a lot of interesting
things". Can you provide examples for one of these interesting things?


Lazy evaluation where the value of something is calculated the first
time it is needed but accessed from some storage if it is needed again.


I do this all the time. It's not very hard and doesn't require any
extra language support, but I would like for there to be an
authoritative list of type slots (autopromise_op s).

import operator

def promise(thunk):
x = []
def promised():
if not x:
x.append(thunk( ))
return x[0]
return promised

autopromise_ops = [x for x in dir(operator) if x.startswith('_ _')]
autopromise_ops += ['__getattribute __', '__call__', '__str__', '__repr__']
autopromise_ops += ['__getattr__', '__setattr__', '__delattr__']

def autopromise(thu nk):
p = promise(thunk)
d = {}
for op in autopromise_ops :
def bindhack(op=op) :
return lambda self, *a, **kw: getattr(p(), op)(*a, **kw)
d[op] = bindhack()
return type('autopromi se', (), d)()

def test():

lis = []

def thunk():
lis.append('ran thunk')
return 'value'

s = autopromise(thu nk)
p = s * 30
assert p == 'value' * 30
p = s * 10
assert p == 'value' * 10
assert lis == ['ran thunk'] # Just once

print 'autopromise sanity test passed'

An autopromise object is good almost everywhere the real one would be,
and usually the only way to tell the difference is to call id or type
on it. The main exception is when the thunk returns a builtin type
(like a string or int) and you want to pass it to a builtin function
that expects a particular type (this would also apply to Python
functions that break duck typing on purpose, but those would just be
getting the breakage they deserve).
Jul 18 '05 #10

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

Similar topics

8
1620
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 tool. But I am confused by their behavior on instance of my class. I can only get the approximate behavior by using class variables. I am looking for something like:
3
2169
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 for a member variable of a Python object. It has to be given a value when defined, or set within a method. But what is the difference between an Attribute of a Class, a Descriptor in a Class, and a Property in a Class?
7
1350
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 example where you set an attribute s to a string and have another attribute l set automatically to its length.
1
1196
by: Chris Leary | last post by:
As I understand it, the appeal of properties (and descriptors in general) in new-style classes is that they provide a way to "intercept" direct attribute accesses. This lets us write more clear and concise code that accesses members directly without fear of future API changes. I love this feature of the language, but find that I still have to call getter/setter methods of module instances. Since module attributes are accessed by way of...
0
9568
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
9398
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
10007
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...
0
9832
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...
1
7375
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
5275
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.