[C++-sig] Re: Re: Re: Re: enum_ exporting implemented

David Abrahams dave at boost-consulting.com
Sat Dec 14 03:37:16 CET 2002


"Mike Rovner" <mike at bindkey.com> writes:

>> >> What do you think enums.color.red + enums.color.green should do?
>> >
>> > Return integer 3.
>
> Now color.red+color.blue doesn't return color(5) but simply 5.
> It's not consistent with color(3) above. 

Maybe color.red + color.green => 3 also, I didn't check.
Oh, of course it is, just like in C++.
If you want a color, you write color(color.red + color.green)

Remember, the goal is to wrap C++ code providing a very similar
interface in Python.

> Unlike C++ Python doesn't have compile-time constant simplification
> (am I right?), so it's an expression.  


> imho propagation of enums to python shall be restricted only to
> explicitly declared constants.

Not until you can show that it causes an problem somehow.

> Sure, but the key difference in constant vs. expresiion.
> Also we can do
>
> enum { x=1, y, z };
>
> Then y+z is 5 which is outside maximum bitrange of that enum and therefore
> is not the same C++ type,

Wrong.  It's the same type, but the behavior is undefined or
unspecified (I forget which).

> but still make sense for flags. 

No... what I meant by flags was:

enum Fu { x = 1, y = 2, z = 4 };

Fu combined = x | y | z;

> So is shall be just integer from python side.

Not over my dead body.

>> > All that is IMHO without pretending to know better.
>>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>
>> Yeah, right!
>
> Can we still discuss it?

OK, but only for a few moments more...

> --------------------
>
> Well may be I just dumb minded (today !? ;))
> Can you kindly explain *Python* semantics of color(3) above.
> It just make no sense for me, though I understand C/C++ heritage,
> etfc...

I don't know what you're asking me to explain. Instead, consider this
code:

    enum Fu { x = 1, y = 2, z = 4 };
    Fu flags = x | y;
    Fu get_flags() { return flags; }
    void set_flags( Fu v ) { flags = v; }

    BOOST_PYTHON_MODULE(test)
    {
       enum_<Fu>("Fu")
            .value("x", x)
            .value("y", y)
            .value("z", z)
            ;
       def("get_flags", get_flags);
       def("set_flags", set_flags);
    }

            ...

    >>> from test import *
    >>> set_flags(get_flags())

Should this work? IMO, yes. If get_flags() returns an int for unnamed
values of Fu, it will not work, since set_flags requires a Fu argument.

-- 
                       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