Live data from Hacker News

Tail recursion in Python

chrispenner.ca

11–20 of 87 posts

Re: Tail recursion in Python

#11

Interesting use of exceptions.

Indeed although generally it's usually a bad idea to misappropriate the exception throwing / handling mechanism for other purposes, as it's probably be less well optimised, performance-wise, than other parts of a VM.

Re: Tail recursion in Python

#12
post #11

Interesting use of exceptions.

Indeed although generally it's usually a bad idea to misappropriate the exception throwing / handling mechanism for other purposes, as it's probably be less well optimised, performance-wise, than other parts of a VM.

not in python. exceptions for flow control are not looked down upon unless it’s gratuitous usage. many frameworks do exactly this.

Re: Tail recursion in Python

#14
post #10

Code snippets you won't see if you have JS disabled: https://gist.github.com/ChrisPenner/c0b3f4feb054daa2f6370d2e... https://gist.github.com/ChrisPenner/c958afbf6e7a763c188d8b83...

JS fully disabled in this day and age?

It's more common than you think.

Re: Tail recursion in Python

#15
Someone recently pointed out to me you can bypass the recursion limit with an inbuilt decorator, because it's basically a memoiser.

lru_cache, from the functools library.

The example given in the docs [0] is:

    import functools

    @functools.lru_cache(maxsize=None)
    def fib(n):
        if n 
[0] https://docs.python.org/3/library/functools.html#functools.l...

Re: Tail recursion in Python

#16
post #11

Earlier quoted context omitted.

Indeed although generally it's usually a bad idea to misappropriate the exception throwing / handling mechanism for other purposes, as it's probably be less well optimised, performance-wise, than other parts of a VM.

not in python. exceptions for flow control are not looked down upon unless it’s gratuitous usage. many frameworks do exactly this.

Even the language itself does this: if a generator that is being processed by a for loop returns (rather than yield), the language will raise a StopIteration exception, which the for loop with catch and use as a signal that it should exit.

Re: Tail recursion in Python

#18
post #10

Code snippets you won't see if you have JS disabled: https://gist.github.com/ChrisPenner/c0b3f4feb054daa2f6370d2e... https://gist.github.com/ChrisPenner/c958afbf6e7a763c188d8b83...

JS fully disabled in this day and age?

opt-in rather then no opt-out?

Re: Tail recursion in Python

#19
post #11

Earlier quoted context omitted.

Indeed although generally it's usually a bad idea to misappropriate the exception throwing / handling mechanism for other purposes, as it's probably be less well optimised, performance-wise, than other parts of a VM.

not in python. exceptions for flow control are not looked down upon unless it’s gratuitous usage. many frameworks do exactly this.

Yes! the more I dive into general py libraries the more I see `try: import pylib2 except: pylib2 = None` etc,

Re: Tail recursion in Python

#20
post #10

Code snippets you won't see if you have JS disabled: https://gist.github.com/ChrisPenner/c0b3f4feb054daa2f6370d2e... https://gist.github.com/ChrisPenner/c958afbf6e7a763c188d8b83...

JS fully disabled in this day and age?

Only on Hacker News :)
Post reply on HN