3

I have the following in a file named seta.py:

def print_name():
    print "hello"

I am doing the following from the interpreter:

import seta

and then

seta.print_name

I would expect the output to be "hello" but it is as follows:

<function print_name at 0x7faffa1585f0>

What am i doing wrong?

5
  • 2
    anyone care to say why they down vote? Obviously in hindsight the question was very easy but down votes like this put people off from asking questions. Commented Jul 9, 2013 at 21:53
  • 3
    +1 to why downvote. It's hard to figure out the problem here if you're completely new to the language, because you don't know what to search the web for in order to figure out your problem. In other words, you don't speak the language necessary to know what to ask. OP gave his code, how he was using it, and the expected output, which is exactly what we ask of others. Commented Jul 9, 2013 at 21:56
  • 2
    I actually think it's fair to downvote this. Invoking a function is basic stuff, which can reasonably be answered with a tutorial or the language standard. Commented Jul 9, 2013 at 22:02
  • 1
    @Marcin And what if you don't know what it means to invoke a function? You'd have to start from scratch and trudge through arithmetic, print statements, types, and data structures before getting to function definitions until you got to that point and read about that - but you wouldn't know what you were looking for until you found it. You might even miss it, because you weren't aware you were doing anything wrong and you've gotten the parens in the def mixed up with the parens for calling, and you won't recognize the difference. Or maybe you've got attribute syntax mixed up. Commented Jul 9, 2013 at 23:07
  • @2rs2ts Clearly OP does know what function invocation is, they are attempting it. Commented Jul 10, 2013 at 11:16

2 Answers 2

7

To call a function you need to add ():

seta.print_name()

Otherwise it'll print the str/repr version of that function object.

Demo:

def func():
    print "Hello, World!"
>>> func                         #Returns the repr version of function object
<function func at 0xb743cb54>
>>> repr(func)
'<function func at 0xb743cb54>'
>>> print func                   #Equivalent to `print str(func)`
<function func at 0xb743cb54>

>>> func()                       #Eureka!
Hello, World!
Sign up to request clarification or add additional context in comments.

1 Comment

Two minor nits: It might be clearer to show func.__repr__(), because that's what you get from typing seta.print_name, not the __str__. And it might be simpler to just use repr(func) instead of func.__repr__() (so you don't need to understand dunder methods). But otherwise, great explanation.
3

If you define a function at the interpreter, and then print the name of the function without the parens, you'll get __repr__ (representation) of the function object, because in Python, functions are objects. That's what you got, but here's a demo.

>>> def foo():
...     return 1
... 
>>> print foo
<function foo at 0x1005fa398>

Parentheses call the function. The call evaluates to the returned result of the function. So when you type foo(), that turns into 1. But without the parentheses, you are just talking about the function itself, not the calling of it. You expected the result of calling it.

As others will point out, you meant to type seta.print_name().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.