Re: Setting a read-only attribute



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

.



Relevant Pages

  • Lisper says to rubyist: gimmie your syntax tree
    ... def trace ... include Tracer ... trace "initializing..." ... Now I could gsub out those trace statements manually (or for a cleaner ...
    (comp.lang.ruby)
  • [PATCH] ftrace: Documentation
    ... use ftrace and the various tracers. ... +Ftrace is an internal tracer designed to help out developers and ... trace: This file holds the output of the trace in a human readable ... irqsoff - traces the areas that disable interrupts and saves off ...
    (Linux-Kernel)
  • Re: Linux 2.6.26.2
    ... -Ftrace is an internal tracer designed to help out developers and ... -tracers that are currently in ftrace is a tracer to trace ... trace: This file holds the output of the trace in a human readable ... irqsoff - traces the areas that disable interrupts and saves off ...
    (Linux-Kernel)
  • [PATCH -v2] ftrace: Documentation
    ... use ftrace. ... +Ftrace is an internal tracer designed to help out developers and ... trace: This file holds the output of the trace in a human readable ... irqsoff - traces the areas that disable interrupts and saves off ...
    (Linux-Kernel)
  • [patch 01/25] ftrace: remove unneeded documentation
    ... There is no ftrace in the 2.6.26 kernel release, ... -Ftrace is an internal tracer designed to help out developers and ... trace: This file holds the output of the trace in a human readable ... irqsoff - traces the areas that disable interrupts and saves off ...
    (Linux-Kernel)