On 2016-11-17 05:40, Dan Sommers wrote:
On Thu, 17 Nov 2016 16:17:51 +1100, Steven D'Aprano wrote:

... factory functions are great. But I'm saying that as the writer of
the library, not the user of the library. Can you imagine expecting
users to do this?

from math import trig
sin = trig.build('sine')
result = sin(0.1)

No, but I could expect users to do this:

    import math # trig functions take radians by default
    math = math.degree_math # but I like degrees

Or to choose from one of these:

    import math # trig functions take radians by default
    import math.degree_math as math # but I like degrees

Or to choose from one of these:

    import math.radian_math as math # use the radians version
    import math.degree_math as math # use the degrees version

The complexity is taken on once, by the library author, who (presumably)
understands the complexity better than the users do.

[snip]
I wonder if it would be possible to make modules callable. You would do this by adding a __call__ function, and this function could be a factory function:

    import math
    degree_math = math('degree')

This would be equivalent to:

    import math
    degree_math = math.__call__('degree')

Also, a possible syntax extension:

    import math('degree') as degree_math

This would be equivalent to:

    import math as __temp__
    degree_math = __temp__.__call__('degree')
    del __temp__

If you wrote:

    import math('degree')

this would be equivalent to:

    import math as __temp__
    math = __temp__.__call__('degree')
    del __temp__

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to