Using Python 2.2.3, I create this script:
[beginning of 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"
return self.it
def __set__(self, obj, it):
print "in set method"
self.somedata = it
return None
## def foo(self):
## return 1
class AnotherObject(object):
def __init__(self):
self.prop = AnObject("snafu")
myClass = AnotherObject()
print myClass.prop
myClass.prop = "foobar"
print myClass.prop
[end of script]
Then I execute it:
C:\Documents and Settings\John\My Documents\Projects\AstroPy4>b
C:\Documents and Settings\John\My Documents\Projects\AstroPy4>python b.py
<__main__.AnObject object at 0x0086D248>
foobar
It doesn't look like the descriptor protocol is getting
invoked at all.
What's happening here?
John Roth 9 1403
"John Roth" <ne********@jhrothjr.com> writes: What's happening here?
cf.
class AnotherObject(object):
prop = AnObject("snafu")
'as
John Roth said the following on 01/08/2004 01:34 PM: Using Python 2.2.3, I create this script:
[beginning of 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" return self.it def __set__(self, obj, it): print "in set method" self.somedata = it return None ## def foo(self): ## return 1
Hm, I don't know __set__ and __get__, there are __getattr__ (or
__getattribute__) and __setattr__ for dynamically assigning attributes.
Or take a look at properties
(http://www.python.org/2.2/descrintro.html#property)
class AnotherObject(object): def __init__(self): self.prop = AnObject("snafu")
myClass = AnotherObject() print myClass.prop
Now just do:
print id(myClass.prop) to see the internal reference.
myClass.prop = "foobar"
Here you bind an immutable string-object to myClass.prop, the object of
type AnObject you have bound before has no further references in the
code and will be garbage collected.
If you create a destructor for AnObject:
def __del__(self):
print "%s.__del__" % self
you will see that this happens immediately.
print myClass.prop
Regards
Mirko
John Roth wrote: Using Python 2.2.3, I create this script:
[beginning of 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" return self.it def __set__(self, obj, it): print "in set method" self.somedata = it return None ## def foo(self): ## return 1
class AnotherObject(object): def __init__(self): self.prop = AnObject("snafu")
myClass = AnotherObject() print myClass.prop myClass.prop = "foobar" print myClass.prop
[end of script]
Then I execute it:
C:\Documents and Settings\John\My Documents\Projects\AstroPy4>b
C:\Documents and Settings\John\My Documents\Projects\AstroPy4>python b.py <__main__.AnObject object at 0x0086D248> foobar
It doesn't look like the descriptor protocol is getting invoked at all.
What's happening here?
The descriptor protocol works on the class, not the instance, so
class AnotherObject(object):
prop = AnObject("snafu")
or something similar should work. This means in particular that you have to
store the property's state in the AnotherObject rather than the AnObject
instance.
Peter
"John Roth" <ne********@jhrothjr.com> writes:
[snippety] It doesn't look like the descriptor protocol is getting invoked at all.
What's happening here?
Descriptors need to be attached to classes.
Cheers,
mwh
--
6. Symmetry is a complexity-reducing concept (co-routines include
subroutines); seek it everywhere.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
"Michael Hudson" <mw*@python.net> wrote in message
news:m3************@pc150.maths.bris.ac.uk... "John Roth" <ne********@jhrothjr.com> writes:
[snippety]
It doesn't look like the descriptor protocol is getting invoked at all.
What's happening here? Descriptors need to be attached to classes.
Arrrgggh! Of course!
John Roth Cheers, mwh
"John Roth" <ne********@jhrothjr.com> writes: It doesn't look like the descriptor protocol is getting invoked at all.
What's happening here?
Try changing the following
class AnotherObject(object): def __init__(self): self.prop = AnObject("snafu")
to
class AnotherObject(object):
prop = AnObject("snafu")
"John Roth" <ne********@jhrothjr.com> wrote in message
news:vv************@news.supernews.com... Using Python 2.2.3, I create this script:
[beginning of 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" return self.it def __set__(self, obj, it): print "in set method" self.somedata = it
Did you mean to set self.it to match the __init__ and __get__ methods?
Or am I missing something about the esoterics of properties?
Terry J. Reedy
"Terry Reedy" <tj*****@udel.edu> wrote in message
news:Na********************@comcast.com... "John Roth" <ne********@jhrothjr.com> wrote in message news:vv************@news.supernews.com... Using Python 2.2.3, I create this script:
[beginning of 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" return self.it def __set__(self, obj, it): print "in set method" self.somedata = it Did you mean to set self.it to match the __init__ and __get__ methods? Or am I missing something about the esoterics of properties?
No, you're not missing anything. This was simply the result
of futzing around trying to get it to work, and has no other
conceptual value at all. The problem turned out to be that
I was putting it in the instance instead of the class. [sigh.]
John Roth Terry J. Reedy
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Joe |
last post by:
Hi,
I want to use the C library function mkstemp. It returns a unix file
descriptor (int) to an open file. How do I convert the file descriptor to a
valid ofstream object?
Thanks,
Joe
|
by: gipsy boy |
last post by:
Given a file descriptor (from a network socket, for instance), I want to
have an iostream that reads/writes to it, so I can push and read data in
the traditional way :
sockStream << "<some...
|
by: CSN |
last post by:
At startup this appears in the log:
WARNING: dup(0) failed after 3195 successes: Bad file
descriptor
What does it mean?
__________________________________________________
|
by: news |
last post by:
Recently our mail from our e-commerce site has been rejected by AOL due
to an IP block because someone was using our PHP scripts to send spam.
Well, I got that fixed.
But our legitimate...
|
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...
|
by: akaarj |
last post by:
I am using Linux gcc 4.1.1 version
I have a file descriptor and I want to have a FILE* pointer to the
file from the file descriptor.
OR
if I can have the file name using the file descriptor.....
|
by: yinglcs |
last post by:
I have a c/c++ program in linux.
I would like to know if I kill my program (e.g. exit(1)), will it
release all the file descriptors my program held (regardless if I call
close(fd) of each file...
|
by: Bill David |
last post by:
I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);
And we can store the file name for later usage.
But I just wonder if...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
|
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...
| |