Setting a read-only attribute
Steve Holden
steve at holdenweb.com
Fri Aug 31 12:14:40 EDT 2007
tleeuwenburg at gmail.com wrote:
> On Aug 31, 6:14 pm, Alexandre Badez <alexandre.ba... at gmail.com> wrote:
>> On Aug 30, 11:35 pm, "tleeuwenb... at gmail.com" <tleeuwenb... at gmail.com>
>> wrote:
>>
>>> I have an object and wish to set an attribute on it which,
>>> unfortunately for me, is read-only.
>>> How can I go about this?
>>> Cheers.
>>> -T
>> Could you show the object you want to set his attribute?
>> Until that, it's difficult to answer to you.
>>
>> PS: If the attribut is on read only, their must a good reason for
>> that ;)
>
> Hi all,
>
> Thanks for all the responses. What I'm trying to do is kludge around
> something. sys.settrace takes a method whose arguments are (frame,
> event, arg). I want to have a tracer class which can be instantiated
> and listen in on these trace calls.
>
> Another way to go about it *might* be to have a module-level list of
> registered Tracer objects which a module-level trace method informs of
> events. It would probably be easier. In fact, I'll go do that.
>
> *That said*, I still think it makes sense to be able to have objects
> register with sys.settrace.
>
> So what I did then was declare a static method with the same pattern
> expected by sys.settrace. I then want to use something like __dict__
> or __setattr__ to give that method a reference to the owning object.
> And this is what I'm trying to do -- declare a static method, then "un-
> static it" by adding a reference to the callable object...
>
> Here's some code:
> ------------------------------------------------------------
>
> import sys
>
>
> class Tracer:
> '''
> Instantiate this in order to access program trace information.
>
> '''
>
> def _getcallback(self):
>
> @staticmethod
> def callback(frame, event, arg):
> print "tracing ...", tracerReference
> #print "line ", frame.f_lineno, frame.f_locals
>
> return callback
>
> def startTrace(self):
> callback = self._getcallback()
> callback.__dict__['tracerReference'] = self
> sys.settrace(callback)
>
>
> def foo(dict):
> for i in range(2):
> pass
>
> if __name__ == "__main__":
> t = Tracer()
> t.startTrace()
> foo({1 : 5})
>
Surely the thing to do, if I understand you, is to declare callback as a
standard method and then pass a reference to a bound method (the most
obvious candidate being self.callback) to sys.settrace().
sholden at bigboy ~/Projects/Python
$ cat test05.py
import sys
class Tracer:
'''
Instantiate this in order to access program trace information.
'''
def callback(self, frame, event, arg):
print "tracing ...", self
print "line ", frame.f_lineno, frame.f_locals
def startTrace(self):
sys.settrace(self.callback)
def foo(dict):
for i in range(2):
pass
if __name__ == "__main__":
t = Tracer()
t.startTrace()
foo({1 : 5})
sholden at bigboy ~/Projects/Python
$ python test05.py
tracing ... <__main__.Tracer instance at 0x7ff2514c>
line 19 {'dict': {1: 5}}
sholden at bigboy ~/Projects/Python
$
Does this do what you want?
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
More information about the Python-list
mailing list