Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'operator': 0.03; 'alain': 0.09; 'subject:while': 0.09; 'python': 0.11; 'def': 0.12; 'class:': 0.16; 'executed,': 0.16; 'i.e.,': 0.16; 'janssen': 0.16; 'means.': 0.16; 'precedence': 0.16; 'subject:recursion': 0.16; 'to:name:python list': 0.16; 'prevent': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; '>>>>': 0.31; 'class': 0.32; 'totally': 0.33; 'skip:_ 10': 0.34; "can't": 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'executing': 0.36; 'false': 0.36; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'how': 0.40; 'then,': 0.60; 'total': 0.65; 'irrelevant': 0.84; 'moves': 0.84; 'procedural': 0.91; 'imagine': 0.93; 'washington': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=BxLyeF+fofCGn6Thqds4JNhGATiV8LZQPcxJw7vUB+E=; b=hP9syZi31P9xsslaIDNpJBD5KZSXXT0lIK9XeGZeWa8gQiKoeLS2f1dmDtHKyVO7CL 7NYsCbqCmLauTgTuQKqSyHt96ao4ceb99Xl9ZF57m84TJo4FiIJiDdn9je+Qegzcd0sE zKUF/KNAUqs01BytZCbuTehlQvFDplXpazT9LrBYdiBba49u4LLeWeF6sarx5IRp/9qN OhLGOcF/BHKhJC/bnCL17YtZ0nrpCG153PpCTmjB5qvYzB+f1KRLD8OhNpGZsk/bgvgd NrnDZCmqF65uHWjm0YOWJntNzqvMwt/fGTZ1lJP6tEuXdf7aBujimLi9fw2UWBRzZuBp +k6Q== MIME-Version: 1.0 X-Received: by 10.180.219.33 with SMTP id pl1mr3819694wic.49.1380747917582; Wed, 02 Oct 2013 14:05:17 -0700 (PDT) In-Reply-To: <524C80B6.3010204@unistra.fr> References: <87had0axxy.fsf@dpt-info.u-strasbg.fr> <524C80B6.3010204@unistra.fr> Date: Wed, 2 Oct 2013 14:05:17 -0700 Subject: Re: Tail recursion to while iteration in 2 easy steps From: Mark Janssen To: Python List Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380747919 news.xs4all.nl 15926 [2001:888:2000:d::a6]:54416 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55379 On Wed, Oct 2, 2013 at 1:23 PM, Alain Ketterlin wrote: > On 10/02/2013 08:59 PM, Mark Janssen wrote: >>>> >>>> def fact(n): return 1 if n <= 1 else n * fact(n-1) >>> >>> How do know that either "<=" or "*" didn't rebind the name "fact" to >>> something else? I think that's the main reason why python cannot apply >>> any procedural optimization >> >> It's called "operator precedence". > > Operator precedence is totally irrelevant here, you misunderstand what > "bind" means. > > Imagine that you call fact(x) where x is an instance of the following class: > > class Strange: > ... > def __le__(dummy): > global fact > fact = someotherfun # this is "binding" > return false > > i.e., executing "n<=1" calls __le__, which rebinds the name "fact" to > someting else. Then, there is no recursive call at all. At the time > fact(x-1) is executed, "fact" is not the same function any more. > > You cannot prevent this in python. No, but you can't prevent a lot of bad moves in python. What you just did there is a total bonehead ("strange"?) of an idea. -- MarkJ Tacoma, Washington