On Oct 24, 5:19 pm, [EMAIL PROTECTED] wrote:
> Tim Golden napisa (a):
>
> > It's only a moment before the metaclass and
> > the Twisted solution come along. :)
>
> I couldn't resist. It's not as elegant as I hoped, but hey, at least
> it memoizes the intermediate classes :-)
>
> class fact_0(object):
>         value = 1
>
> class fact_meta(object):
>         def __new__(cls, name, bases, attrs):
>                 n = attrs['n']
>                 class_name = 'fact_%i' % n
>                 try:
>                         return globals()[class_name]
>                 except KeyError:
>                         new_class = type(class_name, bases, {})
>                         new_class.value = n * fact(n - 1).value
>                         new_class.__str__ = lambda self: str(self.value)
>                         globals()[class_name] = new_class
>                         return new_class
>
> class fact(object):
>         def __new__(self, n_):
>                 class spanish_inquisition(object):
>                         __metaclass__ = fact_meta
>                         n = n_
>                 return spanish_inquisition()
>
> print fact(5)
> print fact(3)
> print fact(1)

Dare I add fact(0)?

120
6
1
<__main__.fact_0 object at 0x011729F0>

Hmm..not sure what that means, but I bet I can't calculate
combinations.

What about fact(-1)?

120
6
1
<__main__.fact_0 object at 0x011729F0>

Traceback (most recent call last):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 31, in <module>
    print fact(-1)
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 21, in __new__
    class spanish_inquisition(object):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 13, in __new__
    new_class.value = n * fact(n - 1).value
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 21, in __new__
    class spanish_inquisition(object):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 13, in __new__
    new_class.value = n * fact(n - 1).value
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 21, in __new__
    class spanish_inquisition(object):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 13, in __new__
    new_class.value = n * fact(n - 1).value
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 21, in __new__
    class spanish_inquisition(object):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 13, in __new__
    new_class.value = n * fact(n - 1).value
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 21, in __new__
    class spanish_inquisition(object):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 13, in __new__
    new_class.value = n * fact(n - 1).value
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 21, in __new__
    class spanish_inquisition(object):
  File "C:/Program Files/PyGTK/Python/user/yet_another_factorial.py",
line 13, i

Wow! An infinite loop in the Traceback. Not quite the exception
I was looking for.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to