In Python, I would do like this:
class foo:
def __init__(self):
self.x = self
Otherwise, now the object is a parameter of itself. How can I do it in common lisp?
(defclass mn ()
((pai :accessor mn-pai
:initarg :pai
:initform self)))
CLOS doesn't have the concept of "this" or "self" because through the use of generic functions, whatever instance is being acted upon is passed as an argument.
So, given your example with the accessor
mn-pai:Here,
instanceis passed as an argument to the accessor.If you created a method:
Again, you see the instance is passed in as the first argument. So, there's always an explicit argument that you use.
Now consider:
So, in a normal class based system, where would you put this method? In a Utility class? Is this a "mn" class method? It sort of defies ready categorization.
Now consider:
if we were to do this:
The second line would fail, as mn2 does not have a
mn-paiaccessor.However, this would work:
Because the
slot-valueis the primitive accessor for CLOS, and both classes have a slot namedpai. But, then you don't get to call the accessor function. Rather you're setting the slot directly. Probably not what you want. And, of course, the names are coincidence. There's no relationship between the classes save their similar names and a shared slot name.But you can then do this:
This works because the runtime will dispatch based on the types of the parameters. We "know"
another-mnis an instance ofmn2because we told the system that it must be when we qualified the argument.But, again, you can see how in a class based system, there's no "place" for this kind of method. We typically just create a Utility class of some kind and stick these in there, or a regular function in the global namespace.
While CLOS has classes, it's not really a class based system.
This also comes up in multi-inheritance scenarios (which CLOS supports). Who is "self" then?