Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Summary</strong>: This is a common way to write compilers, and its just fine here.</p> <p>A very common way to handle this in other languages is by "pattern matching", which is exactly what you've described. I expect that's the name for that <code>case</code> statement in Scala. Its a very common idiom for writing programming language implementations and tools: compilers, interpreters etc. Why is it so good? Because the implementation is completely separated from the data (which is often bad, but generally desirable in compilers).</p> <p>The problem then is that this common idiom for programming language implementation is an anti-pattern in Python. Uh oh. As you can probably tell, this is more a political issue than a language issue. If other Pythonistas saw the code they would scream; if other language implementers saw it, they would understand it immediately.</p> <p>The reason this is an anti-pattern in Python is because Python encourages duck-typed interfaces: you shouldn't have behaviour based on type, but rather they should be defined by the methods that an object has available at run-time. <a href="https://stackoverflow.com/questions/1381845/writing-interpreters-in-python-is-isinstance-considered-harmful/1381936#1381936">S. Lott's answer</a> works fine if you want it to be idiomatic Python, but it adds little.</p> <p>I suspect that your design isn't really duck-typed - its a compiler after all, and classes defined using a name, with a static structure, are pretty common. If you prefer, you could think of your objects as having a "type" field, and <code>isinstance</code> is used to pattern-match based on that type. </p> <p><strong>Addenum:</strong></p> <p>Pattern-matching is probably the number one reason that people love writing compilers etc in functional languages.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload