by rupe

Why is my Python script returning a built in operation error?

It's most likely that you've done something like the following:

def myfunc():
    i=73
    return i
out = myfunc()
print 'the number is: ' + i
The mistake seen here is a common one, and easy to make. In a language such as Python, where variables are not declared before use, and exist as untyped until given data, programmers often forget that as in other languages, variables loaded with specific types of data do have an allowable set of operations for the type.
In the code above, an attempt is made to concatenate two objects in a print statement. However, string concatenation is not allowable with integer types, which is what the return value of myfunc() was implicitly declared as when 73 was assigned to i in the function.
A better way to do this would be as follows...

import string
def myfunc():
   i=73
   return i
out = myfunc()
print 'the number is' + str(i)

I just wanted to add this FQA because a coworker had this problem today, and it seems like a problem that a lot of people who are programming in Python tend to make, then bang their heads against the wall because they forget that some operations are restricted to specific types.

 


 
Read more of   The Yak's Frequently Questioned Answers   (mod.2008-11-08)

430.   How can I migrate my encrypted gpassman files to ascii?   [jake/2006-05-20]
426.   what is pair programming or what is therapy programming?   [jake/2005-08-26]
424.   Where do you get StrickPants?   [strick/2007-06-30]
422.   What is a tofinfant?   [jake/2005-02-18]
417.   What is the slashdot amnesia phenomenon?   [jake/2004-10-29]
399.   What are some common methods of radio-based navigation?   [overcode/2004-01-03]
391.   Is E.B. White of Strunk and White fame the same one who wrote Charlotte's Web?   [overcode/2003-11-03]
339.   R. U. ready to Impeach the Entire Political Mainstream and Dramatically Decrease the Threat of International Terrorism?   [strick/2002-12-06]
321.   what is jesse's first rule of backpacking?   [jesse/2002-08-13]
318.   whats a good webmail system?   [jesse/2002-06-21] ( chota/2003-07-26 )
312.   Who is Bill Gates's (of microsoft fame) ?   [jake/2002-05-01]
299.   What does a SPACEWHALE look like?   [strick/2002-01-12]
298.   what is the equivalent to strace on openBSD?   [jesse/2001-12-28]
285.   I use gnome, my soundcard died, lots of things need sound to run! what do i do?   [jesse/2001-11-13]
275.   whats an FQA?   [jesse/2001-09-28]
267.   Where can I find lava-lamp generated pseudo random numbers?   [rupe/2001-08-16]
167.   What are the hardware specs on a Cybiko?   [rupe/2001-01-24]
157.   In the intro to the Everclear song "A.M. Radio", what does the reference to KHJ Radio, Los Angeles refer to?   [rupe/2001-01-01]
131.   What does Jesse do to anything after a fresh linux install?   [jesse/2000-10-08]
118.   Where can I find out the cost of giving a sasquatch a funeral?   [rupe/2000-09-14]
103.   Where can I find information on how much I'm worth?   [rupe/2000-06-17]
52.   What countries are .HR .SR .TT .LK etc.   [strick/2000-02-07]
45.   Do I have JAVASCRIPT on?   [strick/2000-02-03]
25.   What does CLEC stand for in the telco business?   [simon/2000-01-22] ( jesse/2000-11-22 )
17.   How can I make a didgeridoo for traveling?   [ult/2002-04-20]