On 07.05.20 09:38, Stephen J. Turnbull wrote:

Christopher Barker writes:

  > So while yes, alternate constructors are a common pattern, I don't
  > think they are a common pattern for classes like zip.

That's a matter of programming style, I think.  There's no real
difference between

     zip(a, b, length='checksame')

and

     zip.checksame(a, b)

They just initialize an internal attribute differently, which takes
one of a very few values.

The big difference between these two versions is about usability. A flag
is convenient if the actual value is expected to be determined only at
runtime so you can write `zip(a, b, length=length)`. A distinct function
on the other hand emphasizes the expectation that this behavior is
usually determined when the code is written; it would be awkward to
write `getattr(zip, length)(a, b)`. Both this and the different behavior
of zip-flavors speak in favor of the second, `zip.<mode>` version. One
concern however is that `zip.checksame` looks like `checksame` is a
convenience function of `zip` that doesn't necessarily perform any
zipping; it could only perform the length check and return True or
False. Sure, for general iterators this would not be very useful because
they get consumed in the process but for someone who doesn't know the
details this might not be as obvious. Maybe people start writing code
like this:

    if not zip.checksame(a, b):
        raise ValueError()
    for stuff in zip(a, b):
        ...
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VOLDNSQUXHCDYTPUQZBBMBKNZE7HKCFJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to