4 Replies - 383 Views - Last Post: 11 October 2012 - 07:40 AM Rate Topic: -----

#1 GotYahweh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 09

python def something

Posted 09 October 2012 - 08:33 AM

I am having a issue understanding how def works. I am going through the book learning python the hard way and I can seem to get this down.


here is my code, very simple so I can see if I understand it correctly;
def func1():
    print "This is my first "

def func2():
    print"this is my second"

print func1

print func2


---------------------------------------------------------------------

Should it not print out the func1 print line as well as the func2 print line. Sorry guys I know this is newbie stuff and dont mean to post stupid questions but any help in understanding defining would be great.

This post has been edited by atraub: 10 October 2012 - 06:55 AM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: python def something

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8011
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: python def something

Posted 09 October 2012 - 09:02 AM

def defines a function. It'll execute the statements in the function body and if you tell it to return a value, it'll return that value.

So if you execute func1, it'll print the string:

>> func1()
This is my first


If func1 returned a value (it doesn't), then you would be able to execute it and use its value, for example for printing:
>> def func3():
  return "This is a string returned by func3"
>> print func3()
This is a string returned by func3


This value is is a full-fledged string, and can do everything a string can do:

>> print func3().split(" ")
["This", "is", "a", ...]


However, func1 is a value now: functions are entities in python. So if you tell it to print func1, the value (as you do above), it's going to give you something you don't want, which is something like
<function func1 at HEX_VALUE> where HEX_VALUE is a unique identifier for the function, probably based on the memory location or something else that you don't care about.
Was This Post Helpful? 1
  • +
  • -

#3 GotYahweh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 09

Re: python def something

Posted 09 October 2012 - 12:11 PM

So if I was defining soothing that I wanted printed like above. How would that look? I would have to return it, so the print I added would be useless in this code? This def as been the hardest for me to wrap my head around, I dont know why. Cause to me logically it looks like it should work. But when the code runs you are right, I get a return just like you posted.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8011
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: python def something

Posted 09 October 2012 - 12:34 PM

There are two prints here. When you call func1(), it'll execute the print statement that's inside the body of the function.
When you print func1, you're asking the interpreter to send a string representation of that function object to the console. What it looks like is that hex string.

Now, when you print func1() you'll get something else. Try it - see if you can work out what's going on there.

>>> print func1()


What happens, and why?
Was This Post Helpful? 1
  • +
  • -

#5 GotYahweh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 09

Re: python def something

Posted 11 October 2012 - 07:40 AM

Wow, Ok so in order to print or call my function, I must use () at the end of the statement. so When I did the print func1() it ran the def fine. I knew it was something fairly easy. Im just real new, and Im doing this book and not much Im understanding. Thanks though that was a good way to explain it, and it really helped me. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1