SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    python; an error with example code from book, nested functions

    reading a book on python. there's some example code demonstrating nested scopes. there must be an error with it. i'm not sure how to fix it, not quite sure how it should be.

    Code:
    def multiplier(factor):
    	def multiplyByFactor(number):
    		return number*factor
    	return multiplyByFactor
    example use/output of it also out of the book:

    Code:
    >>> double = multiplier(2) 
    >>> double(5)
    10
    >>> triple = multiplier(3)
    >>> triple(3)
    9
    >>> multiplier(5)(4)
    20
    i suppose there should be a parameter in brackets after the multiplyByFactor call, but i'm still unsure what'd properly correct the code. any guesses/ideas?

    thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry. I misinterpreted this post. I'll try to get back to you later.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, no problem, thanks.

    i am right it's wrong aren't i? the 'return multiplyByFactor' line just returns the function's address it seems which isn't any good; not going to work like that.

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    sorry, ignore this question, it is absolute twoddle. there's nothing wrong with the code from the book, i got confused.

    be good to delete this thread if possible?

    thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •