python can't count!

D

Dominik Kaspar

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(threading.Thread):
def print_it(n):
print n

def run(self):
self.print_it(34)
while 1:
pass

s = Server()
s.start()
 
J

Jay O'Connor

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?

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
...
 
C

Christopher Koppler

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(threading.Thread):
def print_it(n):
print n

def run(self):
self.print_it(34)
while 1:
pass

s = Server()
s.start()

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




--Christopher
 
T

Terry Reedy

Dominik Kaspar said:
i'm wondering if i'm just too stupid for python

Not if you understand the answers you get to the next question ;-)
or if i've missed something fundamentally important
of its semantics.

Yes. instance.method(args) abbreviates
instance-class-or-superclass.method(instance, args). So
class Server(threading.Thread):
def print_it(n):
print n
def run(self):
self.print_it(34)

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
s = Server() ,
s.start()
translates into Thread.start(s). However, after adding method

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

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

Terry J. Reedy
 
K

Kyler Laird

i'm wondering if i'm just too stupid for python or if i've missed
something fundamentally important of its semantics.

The later.

It'll get easier.

--kyler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top