Question about extending tuple

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

    Question about extending tuple

    I wanted to extend tuple but ran into a problem. Here is what I
    thought would work

    class MyTuple(tuple):
    def __init__(self, *args):
    tuple.__init__( self, args)

    x = MyTuple(1,2,3,4 )

    That gives me...

    TypeError: tuple() takes at most 1 argument (4 given).

    However, this call works:

    x = MyTuple((1,2,3, 4))

    I am perplexed only because "args" is a tuple by the definition of
    *args. Anyone?

  • Georg Brandl

    #2
    Re: Question about extending tuple

    abcd schrieb:
    I wanted to extend tuple but ran into a problem. Here is what I
    thought would work
    >
    class MyTuple(tuple):
    def __init__(self, *args):
    tuple.__init__( self, args)
    >
    x = MyTuple(1,2,3,4 )
    >
    That gives me...
    >
    TypeError: tuple() takes at most 1 argument (4 given).
    >
    However, this call works:
    >
    x = MyTuple((1,2,3, 4))
    >
    I am perplexed only because "args" is a tuple by the definition of
    *args. Anyone?
    As an immutable type, tuple makes use of __new__.

    class MyTuple(tuple):
    def __new__(cls, *args):
    return tuple.__new__(c ls, args)

    should work.

    Georg

    Comment

    • Lawrence Oluyede

      #3
      Re: Question about extending tuple

      abcd <codecraig@gmai l.comwrote:
      I wanted to extend tuple but ran into a problem. Here is what I
      thought would work
      I think you should take a look at this to do it properly from the Python
      devs:


      Look for NamedTuple

      --
      Lawrence, oluyede.org - neropercaso.it
      "It is difficult to get a man to understand
      something when his salary depends on not
      understanding it" - Upton Sinclair

      Comment

      • abcd

        #4
        Re: Question about extending tuple

        As an immutable type, tuple makes use of __new__.
        >
        class MyTuple(tuple):
        def __new__(cls, *args):
        return tuple.__new__(c ls, args)
        >
        should work.
        >
        Georg
        strange. not very consistent.

        Comment

        • Georg Brandl

          #5
          Re: Question about extending tuple

          abcd schrieb:
          >As an immutable type, tuple makes use of __new__.
          >>
          >class MyTuple(tuple):
          > def __new__(cls, *args):
          > return tuple.__new__(c ls, args)
          >>
          >should work.
          >>
          >Georg
          >
          strange. not very consistent.
          On the contrary -- __new__ *and* __init__ exist for all types.
          The only difference is where a specific object is initialized, and
          therefore which method you have to override.

          __new__ is a static method (it doesn't need to be declared as one,
          this is done automatically as it predates the introduction of
          staticmethod()) which is called to *construct* an instance.
          This can only be done once for a specific object since each call to
          __new__ will result in a *new* object. In other words, this is
          perfect for immutable objects -- once created, never changed.

          __init__, OTOH, is called on the *instance* to initialize it. Of
          course, this process can be repeated, and is therefore apt for
          mutable objects like lists.

          I hope you see now why it is consistent.

          Georg

          Comment

          • abcd

            #6
            Re: Question about extending tuple

            >
            I hope you see now why it is consistent.
            >
            Georg
            yea that clears it up. thanks.

            Comment

            Working...