Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no casting as the other answers already explained. You can make subclasses or make modified new types with the extra functionality using <strong>decorators</strong>.</p> <p>Here's a complete example (credit to <a href="https://stackoverflow.com/questions/739654/understanding-python-decorators">How to make a chain of function decorators?</a>). You do not need to modify your original classes. In my example the original class is called Working.</p> <pre><code># decorator for logging def logging(func): def wrapper(*args, **kwargs): print func.__name__, args, kwargs res = func(*args, **kwargs) return res return wrapper # this is some example class you do not want to/can not modify class Working: def Do(c): print("I am working") def pr(c,printit): # other example method print(printit) def bla(c): # other example method c.pr("saybla") # this is how to make a new class with some methods logged: class MutantWorking(Working): pr=logging(Working.pr) bla=logging(Working.bla) Do=logging(Working.Do) h=MutantWorking() h.bla() h.pr("Working") h.Do() </code></pre> <p>this will print</p> <pre><code>h.bla() bla (&lt;__main__.MutantWorking instance at 0xb776b78c&gt;,) {} pr (&lt;__main__.MutantWorking instance at 0xb776b78c&gt;, 'saybla') {} saybla pr (&lt;__main__.MutantWorking instance at 0xb776b78c&gt;, 'Working') {} Working Do (&lt;__main__.MutantWorking instance at 0xb776b78c&gt;,) {} I am working </code></pre> <p>In addition, I would like to understand why you can not modify a class. Did you try? Because, as an <strong>alternative</strong> to making a subclass, if you feel dynamic you <em>can</em> almost always modify an old class in place:</p> <pre><code>Working.Do=logging(Working.Do) ReturnStatement.Act=logging(ReturnStatement.Act) </code></pre> <p>Update: <strong>Apply logging to all methods of a class</strong></p> <p>As you now specifically asked for this. You <em>can</em> loop over all members and apply logging to them all. But you need to define a rule for what kind of members to modify. The example below excludes any method with __ in its name .</p> <pre><code>import types def hasmethod(obj, name): return hasattr(obj, name) and type(getattr(obj, name)) == types.MethodType def loggify(theclass): for x in filter(lambda x:"__" not in x, dir(theclass)): if hasmethod(theclass,x): print(x) setattr(theclass,x,logging(getattr(theclass,x))) return theclass </code></pre> <p>With this all you have to do to make a new logged version of a class is:</p> <pre><code>@loggify class loggedWorker(Working): pass </code></pre> <p>Or modify an existing class in place:</p> <pre><code>loggify(Working) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload