On Sun, Dec 05, 2021 at 05:31:58PM -0700, Finn Mason wrote:
> Also, on a kind of side note, what would be a situation where early binding
> is advantageous to late binding? I can't think of one off the top of my
> head.
If your language only has one, early binding is better. You can easily
simulate late binding if your language only gives you early:
def func(arg=None):
# You know the drill...
if arg is None:
arg = expression
And you only pay the cost of call-time evaluation of the expression when
you actually need it to be evaluated at call-time.
But to go the other way is inconvenient and annoying, requiring the use
of a global variable (or some other storage) for every parameter:
FUNC_DEFAULT_VALUE = expression
def func(arg=>None):
if arg is None:
arg = FUNC_DEFAULT_VALUE
And now, your early-bound default still pays the cost of evaluating the
sentinel expression (in this case None), but you also pay the cost of a
global lookup to get the cached value of the expression.
You lose encapsulation (the cached value is no longer part of the
function object) and efficiency.
Compiled languages with good optimizing compilers may be able to
optimize away some of that cost. If your language supports static
storage for functions, you can use that. But in general, without come
sort of language support, simulating early binding in a language which
only provides late binding is not as easy, convenient or efficient as
doing it the other way.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZZRHBB4F3W5FZNAARPXANWOFII5KAITU/
Code of Conduct: http://python.org/psf/codeofconduct/