[C++-sig] Re: static methods

David Abrahams dave at boost-consulting.com
Fri Jan 10 21:06:44 CET 2003


Nikolay Mladenov <nickm at sitius.com> writes:

> I am going to read the PEP.
> But I think I am getting closer with:
>     self& staticmethod(char const* name)
>     {
>         PyObject *method = pytype_check(&PyMethod_Type, ((object)(this->attr(name))).ptr());
>         this->attr(name) = object(handle<>(
>              PyStaticMethod_New(PyMethod_GET_FUNCTION(method) )
>             ));
>         return *this;
>     }

> I guess PyMethod_GET_FUNCTION corresponds to "C.__dict__['f']" since
> it extracts the function from the PyMethod Descriptor .

It's similar, but I would prefer it if you would get it from the
class' __dict__.  One day I may have a reason not to build a true
Python Method wrapper, and use something else instead; that would
break your code.

You can reach the class' dict very simply via:

   detail::downcast<PyTypeObject>(this->ptr())->tp_dict

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list