>  > Python classes can also take parameters.
>
>
> I didn't know that.  I thought the only way to create a Python class
>  is for the Python interpreter to execute Python code that looks like this:
>
>  class Foo(...):
>      ...
>
>  That makes a new class called Foo.  How are you going to make, at
>  runtime, new classes for each of Z/nZ say, for 1 <= n < 10^5, i.e.,
>  something like this in Sage:
>
>   v = [Integers(n) for n in range(1,10^5)]
>
>  I do not think it is possible to concisely create a hundred thousand
>  separate Python classes like that.

FWIW, I think classes in python are "just" instances of class
classobj. This latter class classobj (from new import classobj) is
what implements the semantics of python's "new-style classes".

Also classes can be constructed at runtime, as instances of class
classobj. Moreover, one can also subclass "classobj" or "type" to
produce metaclasses with different semantics (or whatever).

Search for "python metaclass", e.g.

http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html

WRT your example:

sage: def classinteger(m):
...     class A:
...       def __init__(self, n):
...         self.__n = n % m
...       def __repr__(self):
...         return  "%d mod %d" % (self.__n, m)
...     A.__name__ = "classintegers mod %d" % m
...     return A
sage: classinteger(5)(3)
3 mod 5
sage: time v = [classinteger(n) for n in range(1,10^5)]
CPU time: 3.64 s,  Wall time: 4.14 s
sage: time w = [Integers(n) for n in range(1,10^5)]
CPU time: 15.75 s,  Wall time: 16.21 s
sage: v[1256](34251235)
499 mod 1257
sage: w[1256](34251235)
499

Note that classinteger(n) is an instance of class 'classobj' in this
simple example.

sage: type(classinteger(n))
<type 'classobj'>
sage: classinteger(7)
<class __main__.classintegers mod 7 at 0x3697770>

But one could define a metaclass so this class works fine as a
first-class object (e.g. it prints nicely, can define members acting
on the class itself, etc):

I was going to say that using classes built at runtime like this was
fine but not a good option for performance reasons, but the example
above shows the overhead of python class creation time is not so
significant.

I guess the part of creating a class which will take time is the
creation of the dictionary; but then one could have the dictionary
pre-loaded somehow.

If, on the other hand, one doesn't care about parent creation time but
worries about element operation time, it might be possible to, at
class creation time, compile the class in cython so that e.g. the
modulus is a hard-coded compile constant... (don't know if this is of
any advantage, it is just for the sake of an example).


Best, Gonzalo

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to