#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    I think it is like functions as parameters? I'm very new to python.


    how would i correct this... want to allow the user of this function to cast the string to to the correct type by entering the desired type as an argument (so i cant later validate against that type within the function)

    Code:
    def get(message = "a value " cast_func = int):
    	print "Please enter " + message 
    	return cast_func(raw_input())
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    I wonder, whether what you try to do could be useful, as there are already built-in functions (int(), float(); long(), str(), ...) available for converting to appropriate types

    http://docs.python.org/2/library/functions.html

    Even if you manage to write such a function, as I understand, each time based on the context in which the function is being used, it is up to you to provide manually, the corresponding casting function value as the second argument of your defined function. So I don't see any generalization that help avoiding rewriting the same code multiple times.


    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Your function as written is fine except it needs a comma between the parameters.

IMN logo majestic logo threadwatch logo seochat tools logo