[sage-devel] Re: Customizing hash for an instance of ?

Sat, 10 Sep 2011 09:39:07 -0700

Hi Maarten,

On 10 Sep., 15:32, Maarten Derickx <m.derickx.stud...@gmail.com>
wrote:
> You should really learn about metaclasses. The basically allow you to change
> the way classes are created. Normally this is done by type. I.e. the
> following are equivalent.
>
> sage: class Foo(object):
> ....:     def a(self):
> ....:         return 1
> ....:    
> sage: f=Foo()
> sage: f.a()
> 1
>
> sage: Foo=type('Foo',(object,),{'a':lambda self: 1})
> sage: f=Foo()
> sage: f.a()
> 1

Yes, that's about as far as I came. But I'm having problems to do the
same in Cython. And that's essential, since it is about speed:

sage: cython("""
....: def a(self):
....:     return id(self)
....: """)
sage: Foo=type('Foo',(object,),{'a':a})
sage: f=Foo()
sage: f.a()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call
last)

/home/king/SAGE/work/fast_hash/<ipython console> in <module>()

TypeError: a() takes exactly one argument (0 given)


The difference is that in your pure Python example, you get
  sage: f.a
  <bound method Foo.<lambda> of <__main__.Foo object at 0x56bee90>>

However, if one starts with a Python function written in Cython, one
gets
  sage: f.a
  <built-in function a>

How can one achieve that self is passed to the function?

I also know that binding can be mimmicked with a __get__ method.
However, that is too slow as well.

Best regards,
Simon

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to