python can't count!

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

    python can't count!

    hello

    i'm wondering if i'm just too stupid for python or if i've missed
    something fundamentally important of its semantics.
    Why does the following program result in the TypeError: print_it()
    takes exactly 1 argument (2 given)?
    I don't see any function passing or receiving 2 arguments here... am i
    blind?

    import threading

    class Server(threadin g.Thread):
    def print_it(n):
    print n

    def run(self):
    self.print_it(3 4)
    while 1:
    pass

    s = Server()
    s.start()
  • Jay O'Connor

    #2
    Re: python can't count!

    On 2 Oct 2003 08:02:48 -0700, dokaspar@studen t.ethz.ch (Dominik
    Kaspar) wrote:
    [color=blue]
    >hello
    >
    >i'm wondering if i'm just too stupid for python or if i've missed
    >something fundamentally important of its semantics.
    >Why does the following program result in the TypeError: print_it()
    >takes exactly 1 argument (2 given)?
    >I don't see any function passing or receiving 2 arguments here... am i
    >blind?[/color]

    You are missing the hidden passing of 'self' to object methods in
    Python. Methods need to declare 'self' in the parameter list

    ...
    def print_it (self, n):
    print n
    ...




    [color=blue]
    >
    >import threading
    >
    >class Server(threadin g.Thread):
    > def print_it(n):
    > print n
    >
    > def run(self):
    > self.print_it(3 4)
    > while 1:
    > pass
    >
    >s = Server()
    >s.start()[/color]

    Comment

    • Christopher Koppler

      #3
      Re: python can't count!

      On 2 Oct 2003 08:02:48 -0700, dokaspar@studen t.ethz.ch (Dominik
      Kaspar) wrote:
      [color=blue]
      >hello
      >
      >i'm wondering if i'm just too stupid for python or if i've missed
      >something fundamentally important of its semantics.
      >Why does the following program result in the TypeError: print_it()
      >takes exactly 1 argument (2 given)?
      >I don't see any function passing or receiving 2 arguments here... am i
      >blind?
      >
      >import threading
      >
      >class Server(threadin g.Thread):
      > def print_it(n):
      > print n
      >
      > def run(self):
      > self.print_it(3 4)
      > while 1:
      > pass
      >
      >s = Server()
      >s.start()[/color]

      You're calling print_it as an instance method, and thus should have
      def'd it as print_it(self, n)




      --Christopher

      Comment

      • Terry Reedy

        #4
        Re: python can't count!


        "Dominik Kaspar" <dokaspar@stude nt.ethz.ch> wrote in message
        news:62e9c66e.0 310020702.7d0e0 2b7@posting.goo gle.com...[color=blue]
        > i'm wondering if i'm just too stupid for python[/color]

        Not if you understand the answers you get to the next question ;-)
        [color=blue]
        > or if i've missed something fundamentally important
        > of its semantics.[/color]

        Yes. instance.method (args) abbreviates
        instance-class-or-superclass.meth od(instance, args). So
        [color=blue]
        > class Server(threadin g.Thread):
        > def print_it(n):
        > print n
        > def run(self):
        > self.print_it(3 4)[/color]

        translates, in this case, to Server.print_it (self, 34), a call with 2
        args.

        The abbreviation is possible because instances know what class they
        are instances of. The abbreviation is handy because is saves a few
        keystrokes. It is important because is allows you to call a method on
        an instance without specifying (or even necessarily knowing) where in
        the class inheritance hierarchy it is defined, and it allows a call to
        continue to work even when that place changes. For instance, after[color=blue]
        >s = Server()[/color]
        ,[color=blue]
        >s.start()[/color]
        translates into Thread.start(s) . However, after adding method

        def start(self):
        <add code to log start time>
        Tread.start(sel f)

        to Server, the same s.start() call would instead translate to
        Server.start(se lf) with no change to the method call!

        Terry J. Reedy




        Comment

        • Kyler Laird

          #5
          Re: python can't count!

          dokaspar@studen t.ethz.ch (Dominik Kaspar) writes:
          [color=blue]
          >i'm wondering if i'm just too stupid for python or if i've missed
          >something fundamentally important of its semantics.[/color]

          The later.

          It'll get easier.

          --kyler

          Comment

          Working...