On Fri, Sep 5, 2014 at 7:22 AM, Jori Mantysalo <jori.mantys...@uta.fi> wrote:
> Here is the code for is_isomorphic() on class FinitePoset:
>
>         if hasattr(other,'hasse_diagram'):
>             return . . .
>         else:
>             raise ValueError('The input is not a finite poset.')
>
> So for example Posets.PentagonPoset().is_isomorphic("Hello everyone!") gives
> the error message as it should.
>
> But why the code doesn't directly check that `other` is of type FinitePoset?
> In theory there could be some other structure using hasse diagram at back.
> Is there some rationale behind this?

The rational is called "duck typing".  Check out [1], "duck typing is
a style of typing in which an object's methods and properties
determine the valid semantics, rather than its inheritance from a
particular class or implementation of an explicit interface. [...]
When I see a bird that walks like a duck and swims like a duck and
quacks like a duck, I call that bird a duck."

The idea is that somebody could create a completely new different
class that they want to use the above code on.  Instead of having to
explicitly change their code to derive from  FinitePoset, they just
have to make sure it has a hasse_diagram method, that quacks like a
duck.

I am neither for nor against this approach, and haven't even read the
surrounding code mentioned above.  I just thought it would be useful
to add a link to the rationale.

[1] http://en.wikipedia.org/wiki/Duck_typing

>
> --
> Jori Mäntysalo
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-devel+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-devel@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.



-- 
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
wst...@uw.edu

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to