[Python-Dev] constant/enum type in stdlib

Glenn Linderman glenn at nevcal.com
Sun Nov 28 02:04:49 CET 2010


On 11/27/2010 12:56 PM, Glenn Linderman wrote:
> On 11/27/2010 2:51 AM, Nick Coghlan wrote:
>> Not quite. I'm suggesting a factory function that works for any value,
>> and derives the parent class from the type of the supplied value.
>> Nick, thanks for the much better implementation than I achieved; you 
> seem to have the same goals as my implementation. I learned a bit 
> making mine, and more understanding yours to some degree. What I 
> still don't understand about your implementation, is that when adding 
> one additional line to your file, it fails:
>> w = named_value("ABC", z )
>> Now I can understand why it might not be a good thing to make a named 
> value of a named value (confusing, at least), but I was surprised, and 
> still do not understand, that it failed reporting the __new__() takes 
> exactly 3 arguments (2 given).

OK, I puzzled out the error, and here is a "cure" of sorts.
 def __new__(cls, name, value):
 try:
 return base_type.__new__(cls, value)
 except TypeError:
 return base_type.__new__(cls, name, value)
 def __init__(self, name, value):
 self.__name = name
 try:
 super().__init__(value)
 except TypeError:
 super().__init__(name, value)
Probably it would be better for the except clause to raise a different 
type of error ( Can't recursively create named value ) or to cleverly 
bypass the intermediate named value, and simply apply a new name to the 
original value. Hmm... For this, only __new__ need be changed:
 def __new__(cls, name, value):
 try:
 return base_type.__new__(cls, value)
 except TypeError:
 return _make_named_value_type( type( value._raw() ))( 
name, value._raw() )
 def __init__(self, name, value):
 self.__name = name
 super().__init__(value)
Thanks for not responding too quickly, I figured out more, and learned more.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20101127/c2e071a0/attachment.html>


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /