Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Noob question: Is all this typecasting normal?

Reply
Thread Tools

Noob question: Is all this typecasting normal?

 
 
sprad
Guest
Posts: n/a
 
      01-02-2009
I've done a good bit of Perl, but I'm new to Python.

I find myself doing a lot of typecasting (or whatever this thing I'm
about to show you is called), and I'm wondering if it's normal, or if
I'm missing an important idiom.

For example:

bet = raw_input("Enter your bet")
if int(bet) == 0:
# respond to a zero bet

Or later, I'll have an integer, and I end up doing something like
this:

print "You still have $" + str(money) + " remaining"

All the time, I'm going int(this) and str(that). Am I supposed to?
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-02-2009
sprad schrieb:
> I've done a good bit of Perl, but I'm new to Python.
>
> I find myself doing a lot of typecasting (or whatever this thing I'm
> about to show you is called), and I'm wondering if it's normal, or if
> I'm missing an important idiom.


It is normal, although below you make things needlessly complicated.

Python is strongly typed, which is a good thing. It refuses to guess you
mean when you multiply a string with a number. Or how a number is to be
formatted when printed.

> For example:
>
> bet = raw_input("Enter your bet")
> if int(bet) == 0:
> # respond to a zero bet



You might better do

bet = int(raw_input("Enter your bet"))

because then you don't need to later on convert bet again and again.

But *one* conversion you need.

> Or later, I'll have an integer, and I end up doing something like
> this:
>
> print "You still have $" + str(money) + " remaining"


This is more concisely & with much better control over the output-format
(think e.g. digits of a fraction) using string-interpolation. See

http://docs.python.org/library/stdty...ing-operations

for an overview.

In your case, a simple

print "You still have $%i remaining" % bet

does the trick.

Diez
 
Reply With Quote
 
 
 
 
TechieInsights
Guest
Posts: n/a
 
      01-02-2009
You can use the built-in string formatting options and operations.
2.5: http://www.python.org/doc/2.5.2/lib/...q-strings.html
2.6: http://docs.python.org/library/string.html

In essence, you can do:

print "You still have $%i remaining" %(money)

On Jan 2, 2:15*pm, sprad <(E-Mail Removed)> wrote:
> I've done a good bit of Perl, but I'm new to Python.
>
> I find myself doing a lot of typecasting (or whatever this thing I'm
> about to show you is called), and I'm wondering if it's normal, or if
> I'm missing an important idiom.
>
> For example:
>
> bet = raw_input("Enter your bet")
> if int(bet) == 0:
> * * # respond to a zero bet
>
> Or later, I'll have an integer, and I end up doing something like
> this:
>
> print "You still have $" + str(money) + " remaining"
>
> All the time, I'm going int(this) and str(that). Am I supposed to?


 
Reply With Quote
 
vk
Guest
Posts: n/a
 
      01-02-2009
> You might better do
>
> bet = int(raw_input("Enter your bet"))
>
> because then you don't need to later on convert bet again and again.


This is all fine until you give it to an end-user.
This is what I picture:

$ ./script.py
Enter your bet: $10

... or perhaps "ten", "all", or a jillion other tainted inputs.

Python will try to cast these strings, but will slap you with a
ValueError instead (an error of some sort, at least).


There needs to be a "user_io" or "sanitize" module in the standard
library to take care of this stuff.
Like:

import userio

logic = userio.userio()

number = logic.getNumeric("blah: ") # will offer the user a "re-do" in
case of bad input
number = logic.forceGetNumeric("Enter your bet!: ") # even if input is
tainted, will return some number

text = logic.getText("blargh: ") # return all text

text = logic.setValidText("[A-Za-z]")
text = logic.forceGetText("blargh: ") # return some text, strips
invalid chars


.... but there isn't, as far as I know.
 
Reply With Quote
 
Andreas Waldenburger
Guest
Posts: n/a
 
      01-02-2009
On Fri, 2 Jan 2009 14:36:04 -0800 (PST) vk <(E-Mail Removed)> wrote:

> There needs to be a "user_io" or "sanitize" module in the standard
> library to take care of this stuff.
> [snip example]
>

Great idea! +1


> ... but there isn't, as far as I know.

Well, get to it, then.

/W

--
My real email address is constructed by swapping the domain with the
recipient (local part).
 
Reply With Quote
 
r
Guest
Posts: n/a
 
      01-02-2009
> There needs to be a "user_io" or "sanitize" module in the standard
> library to take care of this stuff.

[snip]

+1
You are sooo right. You know, it is easy to forget about such things
after you learn a language, i have written my own input logic, but i
remember my __init__ days with python now and the learning curve.

Every new user will makes much use of raw_input()[or input 3.0] and
has to climb this same little hill every time, you and i do it as
second nature. A small module like you describe would be a great
addition to the standard library, and heck, i would even use it
 
Reply With Quote
 
vk
Guest
Posts: n/a
 
      01-03-2009
> If there were, I would expect it to conform with PEP 8 (get those ugly
> camelCase names outta there


haha, please forgive me.
I'll try and think of some more creative names.

atm, I've got a chem final to study for.
I'll probably post something resembling useful code tomorrow morning.

until then, int(input()) away!
 
Reply With Quote
 
Andreas Waldenburger
Guest
Posts: n/a
 
      01-03-2009
On Fri, 2 Jan 2009 16:16:10 -0800 (PST) vk <(E-Mail Removed)> wrote:

> > If there were, I would expect it to conform with PEP 8 (get those
> > ugly camelCase names outta there

>
> haha, please forgive me.
> I'll try and think of some more creative names.


FYI: The names themselves aren't he problem at all. They just should
be all_lowercase_with_underscores if they're functions or variables.
CamelCase (with initial capital!) is "reserved" for classnames only.

/W

--
My real email address is constructed by swapping the domain with the
recipient (local part).
 
Reply With Quote
 
r
Guest
Posts: n/a
 
      01-03-2009
On Jan 2, 6:26*pm, Andreas Waldenburger <(E-Mail Removed)> wrote:
> On Fri, 2 Jan 2009 16:16:10 -0800 (PST) vk <(E-Mail Removed)> wrote:
>
> > > If there were, I would expect it to conform with PEP 8 (get those
> > > ugly camelCase names outta there *

>
> > haha, please forgive me.
> > I'll try and think of some more creative names.

>
> FYI: The names themselves aren't he problem at all. They just should
> be all_lowercase_with_underscores if they're functions or variables.
> CamelCase (with initial capital!) is "reserved" for classnames only.
>
> /W
>
> --
> My real email address is constructed by swapping the domain with the
> recipient (local part).


FYI camelCase with __init__ capital is called "title case" try this:

>>> 'hello world".title()

 
Reply With Quote
 
Andreas Waldenburger
Guest
Posts: n/a
 
      01-03-2009
On Fri, 2 Jan 2009 16:44:11 -0800 (PST) r <(E-Mail Removed)> wrote:

> On Jan 2, 6:26*pm, Andreas Waldenburger <(E-Mail Removed)> wrote:
> > On Fri, 2 Jan 2009 16:16:10 -0800 (PST) vk <(E-Mail Removed)> wrote:
> >
> > > > If there were, I would expect it to conform with PEP 8 (get
> > > > those ugly camelCase names outta there *

> >
> > > haha, please forgive me.
> > > I'll try and think of some more creative names.

> >
> > FYI: The names themselves aren't he problem at all. They just should
> > be all_lowercase_with_underscores if they're functions or variables.
> > CamelCase (with initial capital!) is "reserved" for classnames only.
> >
> > /W
> >
> > --
> > My real email address is constructed by swapping the domain with the
> > recipient (local part).

>
> FYI camelCase with __init__ capital is called "title case" try this:
>

OK, since we're smartassing anyway: CamelCase refers specifically to
compound words or phrases that are conjoined, that is, written without
spaces between the words, where words are separated by writing their
respective first letters in capitals. Title case however refers to
normal phrases where (space separated) words are capitalized, and no
inner capitals occur (unless of course actual CamelCase words are used
in the phrase).

You even assumed that distinction in your example:
> >>> 'hello world".title()


/W

--
My real email address is constructed by swapping the domain with the
recipient (local part).

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
ArrayList typecasting from binary SQL data =?Utf-8?B?Smlt?= ASP .Net 1 04-11-2005 04:49 PM
Typecasting char pointer to structure venkatesh C++ 1 12-06-2003 06:46 AM
Typecasting operator on simple types vs. classes Nicolay Korslund C++ 7 09-30-2003 04:33 PM
Understanding Typecasting in C++ Kapil Khosla C++ 3 07-20-2003 11:40 AM



Advertisments