ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> edu-sig
edu-sig
[Edu-sig] Advanced or beginner?
by Kirby Urner other posts by this author
Aug 1 2001 6:57AM messages near this date
[Edu-sig] Re: Changing the Division Operator -- PEP 238, rev 1.12 | [Edu-sig] More re: Advanced or beginner?
When teaching Python, do we begin with a treatment of
low level representations of data?  Or is that saved for
a later time?

For example:

   > >> import struct,array,sys
   > >> sys.byteorder
   'little'
   > >> m = array.array('I','THIS')
   > >> m
   array('I', [1397311572L])
   > >> k = [hex(ord(i))[2:] for i in 'THIS']
   > >> k.reverse()
   > >> k
   ['53', '49', '48', '54']
   > >> k = "".join(k)
   > >> k
   '53494854'
   > >> eval('0x'+k)
   1397311572
   > >> struct.pack('i',1397311572)
   'THIS'

Basically, I'm taking the string 'THIS' and converting
it to an integer.  The way this is done internally
corresponds to concatenating 4 hex bytes in reverse
order, i.e. 'T' = 0x54:

   > >> ord('T')
   84
   > >> hex(84)
   '0x54'

Link those bytes to return a bigger number in decimal
form (1397311572).  Then tell struct it's reading an
integer, which it returns as a corresponding string:
lo and behold, our THIS is returned to us.

Kirby



_______________________________________________
Edu-sig mailing list
Edu-sig@[...].org
http://mail.python.org/mailman/listinfo/edu-sig
Thread:
Kirby Urner
Kirby Urner
Tim Peters

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved