Some suggestion for python classes

Jaap Vermeulen (jaap@sequent.com)
Mon, 10 May 93 10:23:59 PDT

Having used the python classes for a bit, I came up with some pieces
that would make life a lot easier when using the classes within
python. They boil down to 3 suggestions. I used some simple
examples.

1) When using a class method, it would be nice if the calling class
would be added as the 1st argument of the method.

First let me explain the nomenclature. A class method is a method
invoked through the class instead of the method. I.e.

class foo:
def bar():
something

Invoking foo.bar() is invoking a class method. You could invoke
the same method as a instance method, foo().bar(), however, it
would result in a 'TypeError: arg cound mismatch' since the
instance itself is automatically prepended to the argument list.
What I propose is to do the same when you invoke the method as a
class method, and prepend the class to the method. This would
result in:

class foo:
def bar(self):
print type(self)

Invoking foo.bar() would result in <type 'class'>, invoking
foo().bar() would result in <type 'instance'>. It is up to the
programmer to correctly code, invoke and identify class method vs.
instance methods (as with a lot of other things in python).

The benefit would be that you can define a class method in a base
class, and still have access to the invoking class. i.e.

class foo:
def new(self):
new_instance = self()
...
return new_instance

class bar(foo):
...

Invoking bar.new() would return an instance of bar. The current
work-around is to invoke the class method as bar.new(bar).

2) It would be nice to have a special operator to implement the
'super' functionality. This functionality allows you to start
searching for a method starting at the base classes, not the
current class. The current workaround is to define a function to
delete the method from the current class (if it's there), retrieve
it using the normal method, and replacing the method in the current
class. This allows you to write:

class bar(foo):
def new(self):
new_instance = super(self, 'new')(self)
...
return new_instance

What I would like to see is some special operator that does this
for you, e.g. using a carrot:

new_instance = self^new()

3) It would be nice to be able to retrieve the name of a class. The
attribute exists, but is empty. I.e. bar.__name__ returns None.
Also, the address operator Guido was talking about would come in
very handy, since you would be able to define something like this
(given that '&' is the address operator):

class bar(foo):
def __repr__(self):
try: return '<class ' + self.__name__ + 'at ' + `&self`
except:
return '<instance ' + self.__class__.__name__ + 'at ' + `&self`

This brings up another point, and that is that __repr__ currently
doesn't work for classes, it would be nice if it did.

Suggestions and comments are welcome.

-Jaap-

--
Jaap Vermeulen					+--------------------------+
						| Sequent Computer Systems |
	Internet : jaap@sequent.com		| Beaverton, Oregon	   |
	Uucp	 : ...uunet!sequent!jaap	+--------------------------+