Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > learnpython.org - an online interactive Python tutorial

Reply
Thread Tools

learnpython.org - an online interactive Python tutorial

 
 
harrismh777
Guest
Posts: n/a
 
      04-22-2011
Heiko Wundram wrote:
> The difference between strong typing and weak typing is best described by:
>
> Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
> [GCC 4.3.4 20090804 (release) 1] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> >>> 1+'2'

> Traceback (most recent call last):
> File "<stdin>", line 1, in<module>
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>> >>>


Yes. And you have managed to point out a serious flaw in the overall
logic and consistency of Python, IMHO.

Strings should auto-type-promote to numbers if appropriate.

This behavior should occur in input() as well. If a 'number' string is
entered and can be converted to a Python number (whatever I mean by that
at the moment) then the string should be converted to a number (int or
float as appropriate) and the input() should return a reference to the
number type ( a value ); otherwise, input() should return the string
entered, or throw a type error.

If an operation like (+) is used to add 1 + '1' then the string should
be converted to int and the addition should take place, returning a
reference to object int (2).


My feelings about this are strongly influenced by my experiences with
the REXX language on IBM's SAA systems--- OS/2 and VM/CMS. In REXX
everything is a string... everything. If a string just happens to be a
REXX number, then it can be manipulated as you might expect for a
number. Neither here, nor there... just that I believe Python could
take advantage of the "Python Number" concept and provide for auto-type
casting of string to number (int or float) as appropriate if the string
meets the Python Number requirements.

Just an idea... again, probably got beat up long before my time, I'm
guessing...


kind regards,
m harris



 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      04-22-2011
On Fri, Apr 22, 2011 at 4:38 PM, harrismh777 <(E-Mail Removed)> wrote:
> My feelings about this are strongly influenced by my experiences with the
> REXX language on IBM's SAA systems--- OS/2 and VM/CMS. In REXX everything is
> a string... everything. If a string just happens to be a REXX number, then
> it can be manipulated as you might expect for a number.


Wow, someone else who knows REXX and OS/2! REXX was the first bignum
language I met, and it was really cool after working in BASIC and
80x86 assembly to suddenly be able to work with arbitrary-precision
numbers!

The "everything is a string, but treat it as a number if you like"
system is rather handy in a few places. I wanted it for my automated
DNS editor - I wanted to concatenate several numbers (from the date,
and the constant "1"), and then, if the resulting number is not
greater than another number (the previous serial), increment. Ahh
well...

I'm not so sure that all strings should autopromote to integer (or
"numeric" generally). However, adding a string and a number _should_
(IMHO) promote the number to string.

print "Hello, "+name+", you have "+credit+" dollars of credit with us."

Okay, that one is probably better done with the % operator, but it
definitely makes logical sense to concatenate numbers and strings as
strings, not to add them as numbers.

Chris Angelico
 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      04-22-2011
harrismh777 wrote:

> Heiko Wundram wrote:
>> The difference between strong typing and weak typing is best described
>> by:
>>
>> Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
>> [GCC 4.3.4 20090804 (release) 1] on cygwin
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> >>> 1+'2'

>> Traceback (most recent call last):
>> File "<stdin>", line 1, in<module>
>> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>>> >>>

>
> Yes. And you have managed to point out a serious flaw in the overall
> logic and consistency of Python, IMHO.
>
> Strings should auto-type-promote to numbers if appropriate.


"Appropriate" is the problem. This is why Perl needs two completely
different kinds of comparison -- one that works as though its operands are
numbers, and one that works as though they're strings. Surprises to the
programmer who picks the wrong one.

Mel.

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      04-22-2011
In article <iorui3$a9g$(E-Mail Removed)>, Mel <(E-Mail Removed)>
wrote:

> > Strings should auto-type-promote to numbers if appropriate.

>
> "Appropriate" is the problem. This is why Perl needs two completely
> different kinds of comparison -- one that works as though its operands are
> numbers, and one that works as though they're strings. Surprises to the
> programmer who picks the wrong one.


Ugh, tell me about it.

The project I'm currently working on used to use PHP (which has the same
auto-promotion semantics as Perl) as the front end to a SQL database.
The PHP code gleefully turned strings into numbers and back again all
over the place, but it all got cleaned up at the database interface
since SQL has strict typing.

We converted the back end database to MongoDB, which does not have
strict typing. We're still cleaning up the mess of inconsistent data
(i.e. string vs integer) that creeps into the database through various
paths. Not to mention 0 vs. '' vs null vs false.

Implicit type conversion can be convenient. But, then again, so can
removing the safety guards from a chain saw.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      04-23-2011
On Fri, 22 Apr 2011 17:08:53 +1000, Chris Angelico <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:

> I'm not so sure that all strings should autopromote to integer (or
> "numeric" generally). However, adding a string and a number _should_
> (IMHO) promote the number to string.
>
> print "Hello, "+name+", you have "+credit+" dollars of credit with us."
>
> Okay, that one is probably better done with the % operator, but it
> definitely makes logical sense to concatenate numbers and strings as
> strings, not to add them as numbers.
>

But what if /I/ want
"A" + 1
to return
"B"

<G>
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
flebber
Guest
Posts: n/a
 
      04-23-2011
On Apr 23, 4:28*pm, Dennis Lee Bieber <(E-Mail Removed)> wrote:
> On Fri, 22 Apr 2011 17:08:53 +1000, Chris Angelico <(E-Mail Removed)>
> declaimed the following in gmane.comp.python.general:
>
> > I'm not so sure that all strings should autopromote to integer (or
> > "numeric" generally). However, adding a string and a number _should_
> > (IMHO) promote the number to string.

>
> > print "Hello, "+name+", you have "+credit+" dollars of credit with us."

>
> > Okay, that one is probably better done with the % operator, but it
> > definitely makes logical sense to concatenate numbers and strings as
> > strings, not to add them as numbers.

>
> * * * * But what if /I/ want
> * * * * * * * * "A" + 1
> to return
> * * * * * * * * "B"
>
> <G>
> --
> * * * * Wulfraed * * * * * * * * Dennis Lee Bieber * * * * AF6VN
> * * * * (E-Mail Removed) * *HTTP://wlfraed.home.netcom.com/


I like what you have done. Was it deliberate that your site teaches
python 2.x code rather than 3.x?
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      04-23-2011
On Wed, Apr 20, 2011 at 20:15, Ron <(E-Mail Removed)> wrote:
> Hey everyone.
>
> I've written an online interactive Python tutorial atop Google App Engine: http://www.learnpython.org.
>
> All you need to do is log in using your Google account and edit the wiki to add your tutorials.
>
> Read more on the website.
>
> Thanks for your help, and I would appreciate if you help me spread the word, and give me feedback on the website.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Nice work! I notice that the "Next Chapter" link does not work.

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com
 
Reply With Quote
 
rantingrick
Guest
Posts: n/a
 
      04-23-2011
On Apr 23, 1:28*am, Dennis Lee Bieber <(E-Mail Removed)> wrote:

> But what if /I/ want
> * * * * * * * * "A" + 1
> to return
> * * * * * * * * "B"



No problem! Python even allows you to create your own functions! I
know, amazing! 8-O

>>> def succ(s):

return chr(ord(s) + 1)

>>> succ('a')

'b'
>>> succ('B')

'C'
>>> succ('Z')

'['

PS: The cases of (s > 255) or (s < 0) will be for the advanced reader
to solve.
 
Reply With Quote
 
rantingrick
Guest
Posts: n/a
 
      04-23-2011
On Apr 22, 1:38*am, harrismh777 <(E-Mail Removed)> wrote:

> Strings should auto-type-promote to numbers if appropriate.


No they should not! We do not want a language to "guess" our
intentions. We are big boys and girls and should be responsible for
own actions.

> This behavior should occur in input() as well. If a 'number' string is
> entered and can be converted to a Python number (whatever I mean by that
> at the moment) then the string should be converted to a number (int or
> float as appropriate) and the input() should return a reference to the
> number type *( a value ); *otherwise, input() should return the string
> entered, or throw a type error.


No, no, no! This is evil! That is what "eval" is for my friend.

> If an operation like (+) is used to add *1 + '1' then the string should
> be converted to int and the addition should take place, returning a
> reference to object int (2).


My god man, have you gone completely insane? 8-O
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      04-23-2011
On Fri, Apr 22, 2011 at 09:38, harrismh777 <(E-Mail Removed)> wrote:
> If an operation like (+) is used to add *1 + '1' then the string should be
> converted to int and the addition should take place, returning a reference
> to object int (2).
>


No, the int 1 should be cast to a string, and the result should be the
string '11'.

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com
 
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
Trouble with CGI code from Code Example 7.3 of the "Python Interactive CGI Tutorial" epsilon Python 4 08-22-2007 02:41 AM
Interactive/Non-interactive ASPX ? WJ ASP .Net 2 02-26-2005 02:54 AM
non Interactive and Interactive AAH Computer Support 0 01-09-2005 04:09 PM
Interactive and non interactive AAH Computer Support 1 01-09-2005 04:01 PM



Advertisments