From: J. Cliff Dyer on
On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:
> self.assertEqual(platform.__builtins__.__class__, dict,
> "platform.__class__ supposed to be dict")
> self.assertEqual(platform.__name__, 'platform' )

The preferred spelling for:

platform.__builtins__.__class__

would be

type(platform.__builtins__)

It's shorter and easier to read, but essentially says the same thing.

You can also use it on integer literals, which you can't do with your
syntax:

>>> type(1)
<type 'int'>
>>> 1.__class__
...
SyntaxError: invalid syntax

Admittedly, this is a trivial benefit. If you're using a literal, you
already know what type you're dealing with.

Cheers,
Cliff

From: Terry Reedy on
On 4/14/2010 11:19 AM, J. Cliff Dyer wrote:
> On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:
>> self.assertEqual(platform.__builtins__.__class__, dict,
>> "platform.__class__ supposed to be dict")
>> self.assertEqual(platform.__name__, 'platform' )
>
> The preferred spelling for:
>
> platform.__builtins__.__class__
>
> would be
>
> type(platform.__builtins__)

Agreed

> It's shorter and easier to read, but essentially says the same thing.
>
> You can also use it on integer literals, which you can't do with your
> syntax:
>
> >>> type(1)
> <type 'int'>
> >>> 1.__class__
> ...
> SyntaxError: invalid syntax

Add the needed space and it works fine.

>>> 1 .__class__
<class 'int'>

A possible use of literal int attributes is for bound mehods:

>>> inc = 1 .__add__
>>> inc(3)
4
>>> inc(3.0)
NotImplemented

Whereas def inc(n): return n+1 is generic and would return 4.0.

Terry Jan Reedy

 | 
Pages: 1
Prev: missing dll follow-up
Next: ctypes question