tag:blogger.com,1999:blog-8550962.post483315006946992371..comments2024-01-27T21:10:48.750-08:00Comments on PyDev adventures: Python tricks: making sure a function is only called onceFabio Zadroznyhttp://www.blogger.com/profile/04202246218394712738noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8550962.post-87599125913378794982013-07-16T04:08:49.006-07:002013-07-16T04:08:49.006-07:00Hi Ron,
I guess that'd be Ok too :)Hi Ron,<br /><br />I guess that'd be Ok too :)Fabio Zadroznyhttps://www.blogger.com/profile/04202246218394712738noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-81358144579456720392013-07-16T00:14:04.937-07:002013-07-16T00:14:04.937-07:00Regarding the decorator example, why bother with t...Regarding the decorator example, why bother with try/finally? I'd assign the _called just before executing the function itself. What do you think?Ron Kleinhttp://heblog.ronklein.co.il/noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-85382666415266896342012-12-13T11:21:47.967-08:002012-12-13T11:21:47.967-08:00Anonymous: I'm not sure if this is called a &#...Anonymous: I'm not sure if this is called a 'final' (final for me is when you can't reassign something).<br /><br />arrg: Yes, this code is python 2.x. For python 3 you'd have to set __code__.<br /><br />Fabio Zadroznyhttps://www.blogger.com/profile/04202246218394712738noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-60823853937155809882012-12-13T10:57:43.032-08:002012-12-13T10:57:43.032-08:00looks like in python 3 this would be:
func.__code_...looks like in python 3 this would be:<br />func.__code__ = (lambda:None).__code__<br /><br />Is that right, or is there a prettier way in 3.x?arrghttps://www.blogger.com/profile/03358287849550369169noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-52535365778749954172012-12-13T08:43:52.892-08:002012-12-13T08:43:52.892-08:00Isn't this also called a "Final"?Isn't this also called a "Final"?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8550962.post-81095723023342248682012-12-10T14:37:51.419-08:002012-12-10T14:37:51.419-08:00it's actually not "call once", it...it's actually not "call once", it's "call many, execute once"Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8550962.post-73934646347452284302012-12-08T02:50:37.083-08:002012-12-08T02:50:37.083-08:00Hi Anonymous :)
Yes, functools.wraps should defin...Hi Anonymous :)<br /><br />Yes, functools.wraps should definitely be used (thanks for the reminder).<br /><br />And Anonymous 2: I agree, that'd be much more fun :)Fabio Zadroznyhttps://www.blogger.com/profile/04202246218394712738noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-40860338047204805372012-12-08T02:49:47.901-08:002012-12-08T02:49:47.901-08:00Hi Adriano, I agree with you (so, in my real use c...Hi Adriano, I agree with you (so, in my real use cases I used the version that sets a flag and not the one with the func_code). <br /><br />Still, I posted it because I believe knowing about func_code has its own uses -- such as providing a different version after a function is called the first time -- maybe to lazy evaluate requisites or some other not so common use-cases :)<br /><br />Fabio Zadroznyhttps://www.blogger.com/profile/04202246218394712738noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-69631534255950415812012-12-07T23:59:58.327-08:002012-12-07T23:59:58.327-08:00For more fun, and in order to improve the debuggin...For more fun, and in order to improve the debugging skills of your co-workers, replace<br /><br />(lambda:None)<br /><br />with<br /><br />random.choice([ob for ob in globals().values() if inspect.isfunction(ob)])Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8550962.post-74336116499507841502012-12-07T20:30:40.035-08:002012-12-07T20:30:40.035-08:00When doing decorators you should also always use f...When doing decorators you should also always use functools.wraps which ensures the docstring is copied and similar issues http://docs.python.org/2/library/functools.html#functools.wrapsAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8550962.post-58460373448344094142012-12-07T08:58:48.039-08:002012-12-07T08:58:48.039-08:00Actually, I would use the version that sets a func...Actually, I would use the version that sets a func.flag, but implemented as a decorator. Once you got rid of the repeated boilerplate, you don't need to make things harder for the readers that don't know about func_code.Adriano Meishttps://www.blogger.com/profile/12482913931299147097noreply@blogger.com