Can be used to access parts of an itcl object, even if it is private.

It is used when the implementation of an object requires callbacks from contexts that are not strictly within the object (e.g., bindings, after callbacks).

This seems very dangerous to me - perhaps you should consider not using this.

[This is used frequently - someone want to explain why and when it is needed?]

Isn't this just the Itcl counterpart to namespace code ?

GWM The namespace code page says that it is a constructor for namespace inscope; inscope says "This command is not expected to be used directly by programmers". Thus I would expect that itcl::code should also not be used by (end user) programmers, although the construct might be used in supplied Tcl packages. The purpose of declaring a variable 'private' is to assure the programmer that there can be no unexpected modifications of the variable other than through the class interface methods.

DKF: That would probably be a mistake. The purpose of itcl::code is to create a token that can be used from outside the object to call back into a method inside the object. There are a number of places you need this sort of thing (after callbacks, trace callbacks, widget callbacks, …). Note that these are places where there is something that is formally outside the object but which is used as part of the implementation of the object, and they are essential to the practical implementation of any object in Tcl (given our current scoping rules).

APW My use of itcl::code is when I have a callback for example for a widget and I want to execute that callback in the "environment" of an itcl class method even if that method is not public. That normally means, that I also install the callback from within that "environment".

GWM I would think that any callback should be a public member as it is being accessed from outside the class (via the event handler). Here is my short example of creating a class which creates a button and handles its callbacks. Each button is a separate object and can be extended to carry any other data specific to that button's callback. Note that the callback command must be in "" so that the parameter $this is substituted at the object/callback creation time.

package require Itcl
itcl::class hellobtn {
    public variable name "No-one"
    constructor {{frame ""}} { 
        pack [ button $frame.hbtn -text "Hello World" -command "$this greet $frame.hbtn" ]

    method greet {btn} { $btn config -background red -text $name        }
foreach btn {"Don't" Press This Button} {
    pack [frame .f$btn]
    hellobtn h$btn .f$btn
    h$btn configure -name "$btn"