Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 23rd, 2012, 12:43 AM
BrandenBobo BrandenBobo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 BrandenBobo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 46 sec
Reputation 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())

Reply With Quote
  #2  
Old December 23rd, 2012, 05:04 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
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

Reply With Quote
  #3  
Old December 23rd, 2012, 09:38 AM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 76 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 18 sec
Reputation Power: 2
Your function as written is fine except it needs a comma between the parameters.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > I think it is like functions as parameters? I'm very new to python.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap