[Python-Dev] "as" keyword woes
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Dec 6 21:17:58 EST 2008
On Sun, 07 Dec 2008 11:27:56 +1000, Nick Coghlan wrote:
> Warren DeLano wrote:
>> In other words we have lost the ability to refer to "as" as the
>> generalized OOP-compliant/syntax-independent method name for casting:
>
> Other possible spellings:
>
> # Use the normal Python idiom for avoiding keyword clashes # and append
> a trailing underscore
> new_object = old_object.as_(class_hint) float_obj = int_obj.as_("float")
> float_obj = int_obj.as_(float_class)
>
> # Use a different word (such as, oh, "cast" perhaps?) new_object =
> old_object.cast(class_hint) float_obj = int_obj.cast("float")
> float_obj = int_obj.cast(float_class)
I don't like "cast", because a cast is an instruction to the compiler to
treat data as some type other than what it was defined as. It doesn't
create a new piece of data. (At least in C-like languages.)
I'd prefer a method like obj.make_from(type) or obj.export_as(type) or
similar. It's more verbose than "as" but its more explicit about what it
does. obj.as(type) is ambiguous, because it could mean any "return a new
object of type made from obj", "return obj inside a wrapper that makes it
behave as if it were type", "convert obj to type in place".
--
Steven
More information about the Python-list
mailing list