Is there something similar to ?: operator (C/C++) in Python?
Charles Krug
cdkrug at worldnet.att.net
Mon Jun 20 10:10:56 EDT 2005
On Mon, 20 Jun 2005 06:36:42 GMT, Ron Adam <rrr at ronadam.com> wrote:
> Ron Adam wrote:
>
>> You might be able to use a dictionary of tuples.
>>
>> call_obj = {(type_obj1,0):obj1a,
>> (type_obj1,0):obj1b,
>> (type_boj2,1):obj2a,
>> (type_obj2,1):obj2b,
>> etc... }
>> call_obj[(type_of_obj,order)]()
>>
>>
>> Regards, Ron
>
> This won't work like I was thinking it would.
>
> But to get back to your is there a ? operator question...
>
> Try this.
>
> def foo():
> return "foo"
>
> def boo():
> return "boo"
>
> print (foo, boo)[1>0]() # prints "boo"
> print (foo, boo)[1<0]() # prints "foo"
>
> Regards,
> Ron
Another thought:
Often complicated conditional logic is a flag that we need to refactor.
An accounting package I built has an official list of approved vendors,
but allows users to provisionally add a new vendor, which is corrected
later.
The bulk of this system only understands, "This document has-a vendor"
with a "vendor factory" that returns the appropriate type of vendor.
All of the logic specific to the specific subclass is internal to the
subclasses themselves.
More information about the Python-list
mailing list