Setting a read-only attribute

tleeuwenburg at tleeuwenburg at
Fri Aug 31 17:30:16 CEST 2007

On Aug 31, 6:14 pm, Alexandre Badez < at> wrote:
> On Aug 30, 11:35 pm, "tleeuwenb... at" <tleeuwenb... at>
> 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):

        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

def foo(dict):
    for i in range(2):

if __name__ == "__main__":
    t = Tracer()
    foo({1 : 5})

More information about the Python-list mailing list