Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: List of Functions Date: Tue, 29 Mar 2016 08:40:04 +1100 Lines: 62 Message-ID: References: <3c44f0f8-d701-463e-bf2c-f5871c51bddf@googlegroups.com> <87bn5z1mkd.fsf@bsb.me.uk> <87mvpjx9kh.fsf@elektro.pacujo.net> <87twjqn2jn.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 4c5IZmxV/qkbjQMZNNwWcguYqHuBGNitX9QfeolbBiTg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'received:209.85.223': 0.03; 'classes,': 0.05; 'cc:addr:python- list': 0.09; 'lambda:': 0.09; 'mutable': 0.09; 'obj': 0.09; 'objects.': 0.09; ':-)': 0.12; 'syntax': 0.13; 'def': 0.13; 'do,': 0.15; '2016': 0.16; 'cls()': 0.16; 'expression"': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'impractical': 0.16; 'instantiate': 0.16; 'lambda': 0.16; 'rather,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'syntactic': 0.16; 'wrote:': 0.16; 'helper': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'java': 0.22; 'anonymous': 0.22; 'function:': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "i've": 0.25; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; 'dan': 0.29; 'innovation.': 0.29; "i'm": 0.30; 'too.': 0.30; 'code': 0.30; 'class.': 0.30; 'skip:s 30': 0.31; 'realize': 0.32; 'statement': 0.32; 'maybe': 0.33; 'run': 0.33; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'subject:List': 0.33; 'tue,': 0.34; 'running': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'functions.': 0.35; 'quite': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:209': 0.38; 'skip:s 40': 0.38; 'or,': 0.38; 'your': 0.60; 'more': 0.63; 'great': 0.63; 'mar': 0.65; 'benefit': 0.66; 'state,': 0.66; 'statement,': 0.66; 'costs': 0.67; 'chrisa': 0.84; "it'd": 0.84; 'to:none': 0.91; 'curse': 0.91 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:cc; bh=ElbYAUUk7LHWb4ixEuSgRcwthAwbWCeVh1gB8w/74iY=; b=rL81aoNcdEdSct0fvGlYfhIDieCzo8Ggb8CqxFClkVLAwSqyn6Upa/GT5TZi0kTf/n kcgvM1QdumbWd3I+k19m14QjI8BxSs7Qq6+SoZcSkCNXEKGfJnn/BbFC7HIkH+J1hUc6 +XGi0z0NhDLOA8NimYbwOnXDNsxAvqDoJJ6A+22vCktePqn6VM6aRgRfkM1pTo+6Zw2H WocBq3W7Ehogau14dg/CoZ48w/Gk4Iu2h3juKE7eMZNYStGMdbex1N3K6rhspuFggcMM tyq+q4tz8gNWKfcXKB2NWB8cPPSiaeyIPGEzkxiptklg72Cpxm+M7UG0bZtO6LI4d100 3+qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=ElbYAUUk7LHWb4ixEuSgRcwthAwbWCeVh1gB8w/74iY=; b=E6zeDxF9gKepb4zbrQWp0oTNDBipoEz72rhVq+Wo54nKu5UomkChQ6ubye3eF3oAUJ y7uCLUVS3uSouZcYFYN6wljrDgvKrcndTpB63MpcJ0AnTgQFrxXuaFRKL9724oyU10pX 2kOZdPpYOId7xSLsqIoaqs3+yNxQt5X2cgx+Om1JizI/lsajL80yC1qRRT/nlzWgQj7S 4ay+je/ALUiIFH4jQph3oLtuJ4HEgQvMHvH212YmTPEJxS+q6ZGg7Mhlmh27YpXqBad8 AmSwioa2Fsj5gtoeOt+YVq0XyMjGQFU4lDO6wL0oO6CZItuThVngKifEmqeQHjESl5u6 U0gA== X-Gm-Message-State: AD7BkJKE3RDcPgiW4++AnAqmBcBzgqmGnRpTFAW8z4LaSFKQ3wcD8sK3WCqWSEQJ7SybhMg6ffysF5RDhzkpYA== X-Received: by 10.107.137.221 with SMTP id t90mr4993790ioi.31.1459201204344; Mon, 28 Mar 2016 14:40:04 -0700 (PDT) In-Reply-To: <87twjqn2jn.fsf@elektro.pacujo.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105926 On Tue, Mar 29, 2016 at 12:40 AM, Marko Rauhamaa wrote: > Dan Sommers : > >> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote: >> >>> As for Python, I don't feel a great need for anonymous functions. >>> However, I keep running into a need for anonymous classes, or, >>> rather, classless objects. Not a biggie. I just create a one-off >>> inner class and instantiate it, but I do appreciate Java's syntactic >>> innovation. >> >> And I always curse Java for having to create an inner class and a >> method when all I need is a simple function. :-) >> >> I think it's Steven D'Aprano who keeps pointing out that you can >> always name your tiny helper functions instead of using lambda: >> >> def some_complex_function(): >> def f(x) = x + 2 >> some_library_that_wants_a_callback(f) >> some_library_that_wants_a_callback(lambda x: x + 2) >> >> Both calls to some_library_that_wants_a_callback run the same. > > Yes, but I've come to realize that I quite often need more than a > function: I need an object with behavior. The solution is to use a > "helper" class. Can you give an example of code that would benefit from a "lambda-class" construct? Since all functions more complicated than "return this expression" need statement syntax in Python, you'd be pretty restricted in what you can do, so I'm thinking that maybe a SimpleNamespace might suffice: from types import SimpleNamespace obj = SimpleNamespace( add2=lambda x: x+2, squared=lambda x: x*x, ) But if "behaviour" involves mutable state, it'd be fiddly to squish that into lambda functions, so a lambda class would be impractical too. What you could perhaps do is this: def one_off(cls): return cls() @one_off class obj: def func1(self): ... def func2(self): ... obj.func1() obj.func2() It's a statement, with all the costs and benefits thereof, but you don't have to have "obj = obj()" at the end. ChrisA