From: Ulrich Eckhardt on
wheres pythonmonks wrote:
> Thanks ... I thought int was a type-cast (like in C++) so I assumed I
> couldn't reference it.

Hopefully somebody correct me if I explain this badly, but I'll take a
shot...


Firstly, "int" is a class. Python doesn't make a distinction between builtin
types and class types like C++, where you e.g. can't derive from builtin
types.

Secondly, the class is callable like a function. When called, it creates an
instance of that class. Therefore, when you create an object it has a
similar syntax as when calling a function.

Lastly, classes are not special. Like any variable or function they can be
referenced by a name or by multiple names, and thus be passed as parameters
to a function.

Cheers!

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

From: Bruno Desthuilliers on
wheres pythonmonks a �crit :
> Thanks ... I thought int was a type-cast (like in C++) so I assumed I
> couldn't reference it.

Python has no C/C++ like "type-cast". "int" is the builtin integer type,
and instanciating an object in Python is done by calling it's type.
Remember that in Python, everything (at least everything you can bind to
a name) is an object (including functions, types etc), and that
functions are just one kind of callable objects (types - aka "classes"
-, methods and a few other builtin objects are callable too, and you can
of course define your own callable type).

HTH
From: Tim Chase on
On 07/28/10 08:15, wheres pythonmonks wrote:
> f( *map(lambda x: int(x), struct.unpack('2s2s2s','123456')))
> 102
>
> 1. There is a way using unpack to get out string-formatted ints?

well, you can use

>>> s = '123456'
>>> [int(s[i:i+2]) for i in range(0, len(s), 2)]
[12, 34, 56]
>>> f(*_)
102

While your "f()" is just a boring placeholder I assume, you could
also write that as

sum(int(s[i:i+2]) for i in range(0, len(s), 2))

and skip creating f() altogether if all it does is add its arguments.

-tkc



From: Steven D'Aprano on
On Wed, 28 Jul 2010 09:35:52 -0400, wheres pythonmonks wrote:

> Thanks ... I thought int was a type-cast (like in C++) so I assumed I
> couldn't reference it.

Python doesn't have type-casts in the sense of "tell the compiler to
treat object of type A as type B instead". The closest Python has to that
is that if you have an instance of class A, you can do this:

a = A() # make an instance of class A
a.__class__ = B # tell it that it's now class B

and hope that it won't explode when you try to use it :/

I make that seem like a dangerous thing to do, but it's not really. There
actually are sensible use-cases for such a thing, you can't do it to
built-ins (which would be dangerous!), and the worst that will happen
with classes written in Python is they'll raise an exception when you try
calling a method, rather than dump core.

Otherwise, all type conversions in Python create a new instance of the
new type, and the conversion functions themselves (int, str, etc.) are
actual type objects which you can pass around to functions:

>>> type(int)
<type 'type'>

Calling int(x) calls the int constructor with argument x, and returns a
new int object. (In the *specific* case of int, it will sometimes cache
small integers and re-use the same one multiple times, but don't count on
that behaviour since it's an implementation-specific optimization.)


--
Steven
From: Carl Banks on
On Jul 28, 7:32 am, Steven D'Aprano <st...(a)REMOVE-THIS-
cybersource.com.au> wrote:
> On Wed, 28 Jul 2010 09:35:52 -0400, wheres pythonmonks wrote:
> > Thanks ... I thought int was a type-cast (like in C++) so I assumed I
> > couldn't reference it.
>
> Python doesn't have type-casts in the sense of "tell the compiler to
> treat object of type A as type B instead". The closest Python has to that
> is that if you have an instance of class A, you can do this:
>
> a = A()  # make an instance of class A
> a.__class__ = B  # tell it that it's now class B
>
> and hope that it won't explode when you try to use it :/

Type casts in C and non-pathlogical C++ don't modify the object they
are casting.

int (and str, float, etc.) is the closest thing to a type cast in
Python.


Carl Banks