On 2/22/18 11:00 AM, bartc wrote:
On 22/02/2018 12:03, bartc wrote:

On the fib(20) test, it suggests using this to get a 30,000 times speed-up:

BTW while doing my tests, I found you could redefine the same function with no error:

def fred():
    pass

def fred():
    pass

def fred():
    pass

For classes too. I was aware you could reassign a different value to such names, but didn't know this applied to def.

I guess the reason is that 'def' is just another statement, and statements can be conditional, so that you can have several versions.
More importantly, "def x" is just a convenient way to express an assignment to x.  It behaves exactly like "x = make_a_function(name='x', ...)" (where I'm waving my hands about what happens in the ellipsis... :)   Just as there is no prohibition on "x = 1; x = 2", there's no prohibition on "def x(); def x()"

Lots of Python statements are assignments to names, with precisely the same referencing and scoping behaviors as conventional assignment. All of these assign to x:

    x = ...
    def x(...)
    def fn(x)
    class x
    import x
    import blah as x
    from blah import x
    for x in ...
    with blah as x
    except Something as x

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

Reply via email to