Follow us on Twitter!
Ideas are far more powerful than guns.
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 29
Guests Online: 22
Members Online: 7

Registered Members: 82895
Newest Member: kevy90
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Kinda dumb python question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-11-08 04:52
Hi to all, this is my first post in the forum, but i've been reading for a while. I am working on learning Python right now and I need some help with this piece of code. I know it's really simple but I can't get the function to run the number of times I want, it just prints the text once. Any help will be appreciated :)


Code
def stronz():
    print "Text"

def do_n(fun, n):
    if n<=0:
        return
    fun
    do_n(fun, n-1)

do_n(stronz(), 5)




Author

RE: Kinda dumb python question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-11-08 05:21
Code
def stronz():
[tab]print "Text"

def do_n(fun, n):
[tab]if n<=0: return
[tab]fun()
[tab]do_n(fun, n-1)

do_n(stronz, 5)




And now the explanation!

Code
>>> def stronz():
...     print "Text"
...
>>> stronz()
Text
>>> stronz
<function stronz at 0x7f94c2ae1938>
>>>





stronz() invokes the function. Now you may ask, "Why is the output of the stronz function not stored in the fun variable?" Well, print writes to stdout.

stronz is the object, as you can see above. The object is what we want to pass to the fun variable. You can then invoke the function with fun().

NOTE: next time you post code, use your text editor and replace all tabs with '[tab]', or some sort of equivalent.
I look forward to the day the forums support white space.




Edited by on 04-11-08 05:26
Author

RE: Kinda dumb python question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-11-08 05:52
Hey, thanks for stopping by. About adding the () when calling fun I had already done it but then I got this error "TypeError: 'NoneType' object is not callable" :/
Author

RE: Kinda dumb python question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-11-08 06:18
alb00z wrote:
Hey, thanks for stopping by. About adding the () when calling fun I had already done it but then I got this error "TypeError: 'NoneType' object is not callable" :/


Copy and paste my code in the previous post. MY CODE WORKS.

Code

[SwartMumba@localhost Desktop]$ cat asdf.py
def stronz():
     print "Text"

def do_n(fun, n):
     if n<=0: return
     fun()
     do_n(fun, n-1)

do_n(stronz, 5)

[SwartMumba@localhost Desktop]$ python asdf.py
Text
Text
Text
Text
Text
[SwartMumba@localhost Desktop]$





Now, that error message you are talking about...
Please read what I fucking post, else there is no point in me posting. To recap...


stronz() invokes the function. Now you may ask, "Why is the output of the stronz function not stored in the fun variable?" Well, print writes to stdout.

stronz is the object, as you can see above. The object is what we want to pass to the fun variable. You can then invoke the function with fun().


Are you ready to put the puzzle together?

Code

>>> def stronz():
...     print "Text"
...
>>> stronz()
Text
>>> a = stronz()
Text
>>> type(a)
<type 'NoneType'>
>>> a = stronz
>>> type(a)
<type 'function'>
>>>





Now look at your error message.

Code

TypeError: 'NoneType' object is not callable








Edited by on 04-11-08 06:25
Author

RE: Kinda dumb python question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-11-08 06:52
I'm sorry man, I totally missed the explanation about the object. I got it now (kinda loud xD), my only excuse is that is 04:50 over here and I should have been sleeping by now. Thanks again.
Author

RE: Kinda dumb python question

SySTeM
Member

Your avatar

Posts: 1524
Location: England, UK
Joined: 27.07.05
Rank:
HBH Guru
Posted on 04-11-08 20:51
SwartMumba wrote:
I look forward to the day the forums support white space.


Done diddly un! Smile


img138.imageshack.us/img138/6527/sig2ak1.jpg
www.hellboundhackers.org/sig/r/2783.png

http://www.elites0ft.com/
Author

RE: Kinda dumb python question

fashizzlepop
Member



Posts: 482
Location: Old folks home.
Joined: 08.04.08
Rank:
Moderate
Posted on 05-11-08 01:44
Go system!


"The definition of insanity is doing the same thing over and over again and expecting different results.
~Albert Einstein~


csullivan.codeinspire.net/images/boomsig2.png
fashizzlepop@gmail.com http://csullivan.codeinspire.net/
Author

RE: Kinda dumb python question

richohealey
Member



Posts: 1022
Location: #!/usr/local/bin/python
Joined: 01.05.06
Rank:
Monster
Posted on 05-11-08 01:58
system_meltdown wrote:
Done diddly un! Smile


I've got fark all coding time at the moment, but I'll give you a hand with this if you want.

Email me about it and we'll devise a plan.




bitchohealey at hotmail dot com skype:richohealey www.psych0tik.net
Author

RE: Kinda dumb python question

SySTeM
Member

Your avatar

Posts: 1524
Location: England, UK
Joined: 27.07.05
Rank:
HBH Guru
Posted on 05-11-08 20:48
richohealey wrote:
system_meltdown wrote:
Done diddly un! Smile


I've got fark all coding time at the moment, but I'll give you a hand with this if you want.

Email me about it and we'll devise a plan.



Um... I said I done it...? :s


img138.imageshack.us/img138/6527/sig2ak1.jpg
www.hellboundhackers.org/sig/r/2783.png

http://www.elites0ft.com/