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(o bject):
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\M y Documents\Proje cts\AstroPy4>b
C:\Documents and Settings\John\M y Documents\Proje cts\AstroPy4>py thon b.py
<__main__.AnObj ect object at 0x0086D248>
foobar
It doesn't look like the descriptor protocol is getting
invoked at all.
What's happening here?
John Roth 9 1420
"John Roth" <ne********@jhr othjr.com> writes: What's happening here?
cf.
class AnotherObject(o bject):
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(o bject): 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(o bject): 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\M y Documents\Proje cts\AstroPy4>b
C:\Documents and Settings\John\M y Documents\Proje cts\AstroPy4>py thon b.py <__main__.AnObj ect 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(o bject):
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********@jhr othjr.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.math s.bris.ac.uk... "John Roth" <ne********@jhr othjr.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********@jhr othjr.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(o bject): def __init__(self): self.prop = AnObject("snafu ")
to
class AnotherObject(o bject):
prop = AnObject("snafu ")
"John Roth" <ne********@jhr othjr.com> wrote in message
news:vv******** ****@news.super news.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.e du> wrote in message
news:Na******** ************@co mcast.com... "John Roth" <ne********@jhr othjr.com> wrote in message news:vv******** ****@news.super news.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 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...
|
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 stuff>" < "\r\n"
How is this possible with the standard header files?
I need this because my objects have xml'ed << operators, but now I can't
figure out how to use them on the actual network stream.
|
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 auto-generated e-mails are getting "deferred" by AOL
now with an error:
Deferred: Bad file descriptor
I can't find anything on their support site about this, nor Googling.
Any ideas?
| |
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...
|
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..
Please help..
|
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 descriptor)?
Thank you.
|
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 we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |