Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's quite something to claim Python has features that basically very few if any languages have, languages that Python has decided to ignore. Python barely even has a workable REPL.


Python is dynamic and everything is ultimately some sort of object (including functions and classes) and can be modified. Taking the features mentioned one by one:

> the ability to redefine functions

In Python you have two options here, you can either update the module to have a new function:

    >>> mymodule.myfunc = mynewfunc
But any existing references to `myfunc` will not be updated (e.g. if you did `import myfunc from mymoudle` instead of `import mymodule`).

Alternatively you can update the function __code__:

    >>> def f1(): return 1
    ... 
    >>> def f2(): return 2
    ... 
    >>> def f3(): return f1()
    ... 
    >>> f3()
    1
    >>> f1.__code__ = f2.__code__
    >>> f3()
    2
> and whole classes at runtime,

You can take a similar approach with Python classes:

    >>> class A: pass
    ... 
    >>> class B(A): pass
    ... 
    >>> b = B()
    >>> A._a = '_a'
    >>> A.a = lambda self: self._a
    >>> b.a()
    '_a'
You can also update SomeClass.__bases__ should you wish.

> the ability to write new functions (/variables/classes etc) in the process of those redefinitions.

As python is dynamic this can all be done at runtime. If you're not in a REPL you can call compile/exec/__import__ from your code.

> Its debuggers are written in Lisp (completely extensible by you at runtime) offering a REPL right at the debug site.

The Python debugger is written in Python and can be switched out. It's python code so like anything else can be changed at runtime. If you want something flashier than pdb you could use the IPython debugger: https://pypi.org/project/ipdb/

Edit: Want to modify an existing running python program but you didn't think to expose Python's dynamic extensibility before hand? No worries, just use parasite and inject arbitrary code into a running python process! https://pyrasite.readthedocs.io/en/latest/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: