Encapsulation in Python

BartC bc at freeuk.com
Mon Mar 14 21:10:38 EDT 2016


On 15/03/2016 00:28, Mark Lawrence wrote:
> On 14/03/2016 23:56, BartC wrote:

>> Anything so terrible about that that Python needs to keep well clear of
>> or that you think its users should be deprived of?
>
> Yes, it is not even valid Python.  Switch has been rejected via at least
> one PEP and from what I see it adds nothing to the language, so let's
> deprive it from people who clearly don't need it in the first place.

Every time you need to test X against more than one other value, then 
you have a potential use for switch.

But yes you can do without switch if you have too. Same for many features.

> I am only rude to people such as yourself who refuse to provide code, in
> fact anything, to support your case.  Your "benchmark" for the switch
> was yet another laughable farce, which only tested the function calls,
> building tuples, running loops, there was nothing to test with respect
> to the actual switch which was meant to be tested.  So just in case you
> missed it above, where is the profile for this test?

I've shown the task. I'm sure you can also do some tests and show us 
some results.

>> I agree with you. But once you've got the language right, then there's
>> no harm looking at performance. A switch statement like the above can be
>> executed in a single byte-code.
>>
>
> Really?  Then please show us all just how it can be done via a patch to
> the cPython code on the bug tracker.

The one-byte-code switch works when all case expressions are known at 
compile-time. It makes use of a jump-table within the byte-code.

The total sequence will be more than one byte-code, typically:

LOAD_FAST                The index
SWITCH                   Jump to the right label
....
L5:                      One of multiple labels
  ...                     Deal with the code in this branch
JUMP_ABSOLUTE            Break out of the switch

...                      Provision is needed for the jump-table

But only one is needed for testing and dispatch. Now I've sketched it 
out, perhaps you can fill in the details for yourself... (I'm not 
getting involved in CPython development.)


-- 
Bartc



More information about the Python-list mailing list