[Tutor] replacement for constants from other languages in Python?

Kent Johnson kent37 at tds.net
Thu Jan 6 14:08:06 CET 2005


Scott W wrote:
> Kent Johnson wrote:
>> MINVERSION = repr(1.5)
>> should work just fine. It will give the same result as the more readable
>> MINVERSION = '1.5'
> 
> 
> Ok, this would make a bit more sense RE: repr()- in one of the resources
> I found, it seemed to state that repr(x) was converting x into a numeric
> representation, ala atoi() and friends.  

 From the Library Reference section 2.1 Built-in Functions
http://docs.python.org/lib/built-in-funcs.html :

repr(  	object)
     Return a string containing a printable representation of an object...For many types, this 
function makes an attempt to return a string that would yield an object with the same value when 
passed to eval().

So repr(x) will always be a string, and eval(repr(x)) will sometimes == x.

int() and float() convert from strings to numbers.

> OK, that's helpful.  I can see the advantages of several of python's
> mechanisms, such as dir() as well as treating everything like an
> 'interactive object to determine existing methods and attributes (dir()
> and your example of hasattr().  Pretty cool...now to go from libc/POSIX
> to an entirely new and (mostly) different set of core libs and
> functionality.. ;-)  Actually, that IS pretty nice, can perhaps take the
> place of some(most?) of features.h, unistd.h and friends....which is
> really what I want, regardless of the example...or having to 'roll my
> own'.  Very cool.

I'm not familiar with libc or POSIX but much of the os module is a pretty thin layer over the native 
C libs so it might not be so far off of what you are familiar with. OTOH the use of fundamental data 
types like strings and lists is (woohoo!) much different than C or C++.

Kent


More information about the Tutor mailing list