Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-06-01 Thread Paul Moore
2008/6/1 Guido van Rossum [EMAIL PROTECTED]:
 The case for String has already been made.

 Actually I'm not sure. One you know that isinstance(x, String) is
 true, what can you assume you can do with x?
[...]
 Right. I'm now beginning to wonder what exactly you're after here --
 saying that something is an X without saying anything about an API
 isn't very useful. You need to have at least *some* API to be able to
 do anything with that knowledge.

Apologies to Raymond if I'm putting words into his mouth, but I think
it's more about *not* doing things with the type - a String is a
Sequence that we don't wish to iterate through (in the flatten case),
so the code winds up looking like

for elem in seq:
if isinstance(elem, Sequence) and not isinstance(elem, String):
recurse into the element
else:
deal with the element as atomic

This implies that other empty abstract types aren't useful, though,
as they are not subclasses of anything else...

Paul.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-06-01 Thread Guido van Rossum
On Sun, Jun 1, 2008 at 6:57 AM, Paul Moore [EMAIL PROTECTED] wrote:
 2008/6/1 Guido van Rossum [EMAIL PROTECTED]:
 The case for String has already been made.

 Actually I'm not sure. One you know that isinstance(x, String) is
 true, what can you assume you can do with x?
 [...]
 Right. I'm now beginning to wonder what exactly you're after here --
 saying that something is an X without saying anything about an API
 isn't very useful. You need to have at least *some* API to be able to
 do anything with that knowledge.

 Apologies to Raymond if I'm putting words into his mouth, but I think
 it's more about *not* doing things with the type - a String is a
 Sequence that we don't wish to iterate through (in the flatten case),
 so the code winds up looking like

for elem in seq:
if isinstance(elem, Sequence) and not isinstance(elem, String):
recurse into the element
else:
deal with the element as atomic

I thought that was he meant too, until he said he rejected my offhand
suggestion of Atomic with these words: Earlier in the thread it was
made clear that that atomicity is not an intrinsic property of a type;
instead it varies across applications [...]

 This implies that other empty abstract types aren't useful, though,
 as they are not subclasses of anything else...

There's a thread on this out now I believe.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Raymond Hettinger
ISTM, the whole reason people are asking for a String ABC is so you can write isinstance(obj, String) and allow registered 
string-like objects to be accepted.


The downside is that everytime you want this for a concrete class or type, it is necessary to write a whole new ABC listing all of 
the required methods.  Also, you have to change all of the client code's isinstance tests from concrete to abstract.


I propose a simpler approach.  Provide an alternative registration function that overrides isinstance() so that objects can 
explicitly fake any concrete type:


 s = UserString('whiffleball')
 print isinstance(s, str)-- False
 register_lookalike(UserString, str)
 print isinstance(s, str)-- True

Besides saving us from writing tons of new ABCs, the approach works with existing code that already uses isinstance() with concrete 
types.


The ABCs that would remain are ones that are truly abstract, that define a generic interface (like mappings and sequences) and ones 
that offer some useful mixin behavior.  The remaining ABCs are ones where you have a fighting chance of actually being able to 
implement the interface (unlike String where it would be darned tricky to fully emulate encode(), split(), etc.)


This would completely eliminate the need for numbers.Integral for example.  AFAICT, its sole use case is to provide a way for 
numeric's integral types (like int8, int32) to pass themselves off as having the same API as regular ints.  Unfortunately, the 
current approach requires all consumer code to switch from isinstance(x,int) to isinstance(x,Integral).  It would be more useful if 
we could simply write register_lookalike(x,int) and be done with it (no need for numbers.py and its attendant abc machinery).


If we don't do this, then String won't be the last request.  People will want Datetime for example.  Pretty much any concrete type 
could have a look-a-like that wanted its own ABC and for all client code to switch from testing concrete types.



Raymond






___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Guido van Rossum
I'm willing to meet you halfway. I really don't want isinstance(x,
str) to return True for something that doesn't inherit from the
concrete str type; this is bound to lead to too  much confusion and
breakage. But I'm fine with a String ABC (or any other ABC, e.g.
Atomic?) that doesn't define any methods but can be used for type
testing. How about that?

--Guido

On Sat, May 31, 2008 at 5:25 AM, Raymond Hettinger [EMAIL PROTECTED] wrote:
 ISTM, the whole reason people are asking for a String ABC is so you can
 write isinstance(obj, String) and allow registered string-like objects to be
 accepted.

 The downside is that everytime you want this for a concrete class or type,
 it is necessary to write a whole new ABC listing all of the required
 methods.  Also, you have to change all of the client code's isinstance tests
 from concrete to abstract.

 I propose a simpler approach.  Provide an alternative registration function
 that overrides isinstance() so that objects can explicitly fake any concrete
 type:

  s = UserString('whiffleball')
  print isinstance(s, str)-- False
  register_lookalike(UserString, str)
  print isinstance(s, str)-- True

 Besides saving us from writing tons of new ABCs, the approach works with
 existing code that already uses isinstance() with concrete types.

 The ABCs that would remain are ones that are truly abstract, that define a
 generic interface (like mappings and sequences) and ones that offer some
 useful mixin behavior.  The remaining ABCs are ones where you have a
 fighting chance of actually being able to implement the interface (unlike
 String where it would be darned tricky to fully emulate encode(), split(),
 etc.)

 This would completely eliminate the need for numbers.Integral for example.
  AFAICT, its sole use case is to provide a way for numeric's integral types
 (like int8, int32) to pass themselves off as having the same API as regular
 ints.  Unfortunately, the current approach requires all consumer code to
 switch from isinstance(x,int) to isinstance(x,Integral).  It would be more
 useful if we could simply write register_lookalike(x,int) and be done with
 it (no need for numbers.py and its attendant abc machinery).

 If we don't do this, then String won't be the last request.  People will
 want Datetime for example.  Pretty much any concrete type could have a
 look-a-like that wanted its own ABC and for all client code to switch from
 testing concrete types.


 Raymond






 ___
 Python-Dev mailing list
 Python-Dev@python.org
 http://mail.python.org/mailman/listinfo/python-dev
 Unsubscribe:
 http://mail.python.org/mailman/options/python-dev/guido%40python.org




-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Nick Coghlan

Raymond Hettinger wrote:
If we don't do this, then String won't be the last request.  People will 
want Datetime for example.  Pretty much any concrete type could have a 
look-a-like that wanted its own ABC and for all client code to switch 
from testing concrete types.


If I remember rightly, the machinery in the ABC's to support 
registration slows down some other operations with the types - do we 
want to pay that price all the time?


If we do, then it may be a matter of moving some of the registration 
machinery from ABCMeta up into type itself.


Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://www.boredomandlaziness.org
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Guido van Rossum
On Sat, May 31, 2008 at 6:41 PM, Raymond Hettinger [EMAIL PROTECTED] wrote:
 From: Guido van Rossum [EMAIL PROTECTED]
 I'm willing to meet you halfway. I really don't want isinstance(x,
 str) to return True for something that doesn't inherit from the
 concrete str type; this is bound to lead to too  much confusion and
 breakage.

 Probably true.  It was an attractive idea though.  Unless all client
 code converts all its isinstance() tests from concrete to abstract,
 life is going to be tough for people writing look-alike classes
 which will have limited applicability.

I'd rather require that people rewrite their code to benefit from some
new piece of functionality than foisting it upon them regardless,
breaking some perfectly fine working in the process. This is how we've
always done it.

 But I'm fine with a String ABC (or any other ABC, e.g.
 Atomic?) that doesn't define any methods but can be used for type
 testing. How about that?

 That's progress!  It makes abstract substitution possible while
 still saving us a lot of code and avoiding over-specification.
 I propose the following empty abstract classes: String, Datetime, Deque,
 and Socket.

Sounds like a mini-PEP is in place. It should focus on the code to
actually define these and the intended ways to use them.

 -1 on Atomic though.  Earlier in the thread it was made clear that
 that atomicity is not an intrinsic property of a type; instead it varies
 across applications (when flattening email folders, a multi-part mime
 message is atomic, but when flattening individual messages, a
 multi-part mime message is just another nested container, part
 of the tree, not one of the leaves).

Fine, it was just an idle thought.

 Are you open to considering numbers.Integral to be one of the
 new empty abstract classes?  That would make it much easier
 for objects wanting to pass themselves as integers.  As it stands
 now, an aspiring Integral class is required to implement a number
 of  arcana including:  __rxor__, __rrshift__, __pow__, __invert__,
 __index__, and __long__.

I don't think Integer should be completely abstract (what good is an
int you can't add 1 to?) but I could be amenable to reducing the set
of required operations (which could then resurface as a separate ABC).
Please write another mini-PEP. Where did you see __long__? That seems
a mistake (at least in 3.0).

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Raymond Hettinger

From: Guido van Rossum [EMAIL PROTECTED]

I'm willing to meet you halfway. I really don't want isinstance(x,
str) to return True for something that doesn't inherit from the
concrete str type; this is bound to lead to too  much confusion and
breakage.  


Probably true.  It was an attractive idea though.  Unless all client
code converts all its isinstance() tests from concrete to abstract,
life is going to be tough for people writing look-alike classes
which will have limited applicability.



But I'm fine with a String ABC (or any other ABC, e.g.
Atomic?) that doesn't define any methods but can be used for type
testing. How about that?


That's progress!  It makes abstract substitution possible while
still saving us a lot of code and avoiding over-specification.  

I propose the following empty abstract classes:  
   String, Datetime, Deque, and Socket.


-1 on Atomic though.  Earlier in the thread it was made clear that
that atomicity is not an intrinsic property of a type; instead it varies
across applications (when flattening email folders, a multi-part mime 
message is atomic, but when flattening individual messages, a

multi-part mime message is just another nested container, part
of the tree, not one of the leaves).

Are you open to considering numbers.Integral to be one of the
new empty abstract classes?  That would make it much easier
for objects wanting to pass themselves as integers.  As it stands
now, an aspiring Integral class is required to implement a number
of  arcana including:  __rxor__, __rrshift__, __pow__, __invert__,
__index__, and __long__.  



Raymond
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Raymond Hettinger

[Raymond]

I propose the following empty abstract classes: String, Datetime, Deque,
and Socket.


[GvR]

Sounds like a mini-PEP is in place. It should focus on the code to
actually define these and the intended ways to use them.


Okay, will run a Google code search to see if real code exists that
runs isinstance tests on the concrete types.   Since the new classes 
are very lightweight (completely empty), these probably only need
minimal justification.  

The case for String has already been made.  And the concept of a 
Socket is already fully abstract. Not sure I really care about Deque.  


The Datetime.class is tricky.  The existence of many implementations
of date/time routines indicates that there is a need; however, they
don't share the API so they likely won't fit under a common umbrella.




Are you open to considering numbers.Integral to be one of the
new empty abstract classes?  That would make it much easier
for objects wanting to pass themselves as integers.  As it stands
now, an aspiring Integral class is required to implement a number
of  arcana including:  __rxor__, __rrshift__, __pow__, __invert__,
__index__, and __long__.


I don't think Integer should be completely abstract (what good is an
int you can't add 1 to?) but I could be amenable to reducing the set
of required operations (which could then resurface as a separate ABC).
Please write another mini-PEP. 


Okay.  Will propose to remove the bit flipping methods and anything
else that doesn't seem essential to integeriness.  Will take a look at
the integral types in numeric to see what that actually implement.




Where did you see __long__? That seems
a mistake (at least in 3.0).


It's the first listed abstract method in the Py2.6 code.


Raymond
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Alternative to more ABCs [was:] Iterable String Redux (aka String ABC)

2008-05-31 Thread Guido van Rossum
On Sat, May 31, 2008 at 8:09 PM, Raymond Hettinger [EMAIL PROTECTED] wrote:
 [Raymond]

 I propose the following empty abstract classes: String, Datetime,
 Deque,
 and Socket.

 [GvR]

 Sounds like a mini-PEP is in place. It should focus on the code to
 actually define these and the intended ways to use them.

 Okay, will run a Google code search to see if real code exists that
 runs isinstance tests on the concrete types.

I wasn't asking for existing code -- I was asking for the code you
propose to add to abc.py (or wherever).

 Since the new classes are
 very lightweight (completely empty), these probably only need
 minimal justification.

Again, in a mini-PEP I'm not so much looking for justification but for
a precise spec.

 The case for String has already been made.

Actually I'm not sure. One you know that isinstance(x, String) is
true, what can you assume you can do with x?

 And the concept of a Socket is already fully abstract.

Can you elaborate? There's a very specific API that is assumed of
sockets. The code that creates sockets is usually pretty close to the
code that consumes it. There are some major classes that cut right
through the APIs though: connection or listening (the latter being
something on which you call accept()), stream or datagram, and as a
special case of stream SSL and the like.

 Not sure I really care about Deque.
 The Datetime.class is tricky.  The existence of many implementations
 of date/time routines indicates that there is a need; however, they
 don't share the API so they likely won't fit under a common umbrella.

Right. I'm now beginning to wonder what exactly you're after here --
saying that something is an X without saying anything about an API
isn't very useful. You need to have at least *some* API to be able to
do anything with that knowledge.

 Are you open to considering numbers.Integral to be one of the
 new empty abstract classes?  That would make it much easier
 for objects wanting to pass themselves as integers.  As it stands
 now, an aspiring Integral class is required to implement a number
 of  arcana including:  __rxor__, __rrshift__, __pow__, __invert__,
 __index__, and __long__.

 I don't think Integer should be completely abstract (what good is an
 int you can't add 1 to?) but I could be amenable to reducing the set
 of required operations (which could then resurface as a separate ABC).
 Please write another mini-PEP.

 Okay.  Will propose to remove the bit flipping methods and anything
 else that doesn't seem essential to integeriness.  Will take a look at
 the integral types in numeric to see what that actually implement.



 Where did you see __long__? That seems
 a mistake (at least in 3.0).

 It's the first listed abstract method in the Py2.6 code.

That actually makes sense -- correct interoperability with longs
probably requires that.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com