Conflicting needs for __init__ method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Finney

    #16
    Re: Conflicting needs for __init__ method

    "BJörn Lindqvist" <bjourne@gmail. comwrites:
    import rational
    rational.ration al(45)
    rational.ration al(45.0)
    rational.ration al([45, 45.5])
    >
    def rational(obj):
    initers = [(int, from_int), (basestring, from_str), (list, from_list)]
    for obj_type, initer in initers:
    if isinstance(obj, obj_type):
    return initer(obj)
    raise ValueError("Can not create a rational from a %r" % type(obj).__nam e__)
    You've just broken polymorphism. I can't use your factory function
    with an instance of my custom type that behaves like a list, but is
    not derived from list (or a non-'int' int, or a non-'basestring'
    string).

    Use the supplied value as you expect to be able to use it, and catch
    the exception (somewhere) if it doesn't work. That will allow *any*
    type that exhibits the correct behaviour, without needlessly
    restricting it to a particular inheritance.

    --
    \ "This sentence contradicts itself -- no actually it doesn't." |
    `\ -- Douglas Hofstadter |
    _o__) |
    Ben Finney

    Comment

    • =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

      #17
      Re: Conflicting needs for __init__ method

      On 1/16/07, Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
      "BJörn Lindqvist" <bjourne@gmail. comwrites:
      >
      import rational
      rational.ration al(45)
      rational.ration al(45.0)
      rational.ration al([45, 45.5])

      def rational(obj):
      initers = [(int, from_int), (basestring, from_str), (list, from_list)]
      for obj_type, initer in initers:
      if isinstance(obj, obj_type):
      return initer(obj)
      raise ValueError("Can not create a rational from a %r" % type(obj).__nam e__)
      >
      You've just broken polymorphism. I can't use your factory function
      with an instance of my custom type that behaves like a list, but is
      not derived from list (or a non-'int' int, or a non-'basestring'
      string).
      Indeed, but I do not think that is fatal. There are many functions in
      Pythons stdlib that breaks duck typing exactly like that.
      Use the supplied value as you expect to be able to use it, and catch
      the exception (somewhere) if it doesn't work. That will allow *any*
      type that exhibits the correct behaviour, without needlessly
      restricting it to a particular inheritance.
      Can you show an example of that? It seems like that approach would
      lead to some very convoluted code.

      --
      mvh Björn

      Comment

      • Ben Finney

        #18
        Re: Conflicting needs for __init__ method

        "BJörn Lindqvist" <bjourne@gmail. comwrites:
        On 1/16/07, Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
        Use the supplied value as you expect to be able to use it, and
        catch the exception (somewhere) if it doesn't work. That will
        allow *any* type that exhibits the correct behaviour, without
        needlessly restricting it to a particular inheritance.
        >
        Can you show an example of that? It seems like that approach would
        lead to some very convoluted code.
        Perhaps so. I don't recommend either of those approaches; I recommend,
        instead, separate factory functions for separate input types.

        --
        \ "When I was crossing the border into Canada, they asked if I |
        `\ had any firearms with me. I said, 'Well, what do you need?'" |
        _o__) -- Steven Wright |
        Ben Finney

        Comment

        • Chuck Rhode

          #19
          Re: Conflicting needs for __init__ method

          Ben Finney wrote this on Wed, Jan 17, 2007 at 08:27:54PM +1100. My reply is below.
          I recommend, instead, separate factory functions for separate input
          types.
          Uh, how 'bout separate subclasses for separate input types?

          --
          ... Chuck Rhode, Sheboygan, WI, USA
          ... Weather: http://LacusVeris.com/WX
          ... 7° — Wind SW 10 mph

          Comment

          • Ben Finney

            #20
            Re: Conflicting needs for __init__ method

            Chuck Rhode <CRhode@LacusVe ris.comwrites:
            Ben Finney wrote:
            >
            I recommend, instead, separate factory functions for separate
            input types.
            >
            Uh, how 'bout separate subclasses for separate input types?
            The resulting object in each case would be the same type ('Rational'),
            with the same behaviour. Subclassing would make sense only if the
            resulting objects needed to have different behaviour in each case.

            --
            \ "The power of accurate observation is frequently called |
            `\ cynicism by those who don't have it." -- George Bernard Shaw |
            _o__) |
            Ben Finney

            Comment

            Working...