Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: can't add variables to instances of built-in classes Date: Thu, 21 Jul 2016 12:29:38 +1000 Lines: 48 Message-ID: References: <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> <578b9e62$0$22141$c3e8da3$5496439d@news.astraweb.com> <3e934f83-2076-407d-b85b-686ecdec6ff8@googlegroups.com> <2bc29306-ac90-4424-84c2-64be494c5a75@googlegroups.com> <230d1aff-b1a3-4f16-b9a6-b5d9fd07e4b4@googlegroups.com> <62dd86d4-74f7-4e97-a29c-7e45aced77d9@googlegroups.com> <18acf908-b761-4a60-8d7c-5e4576b03320@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de nPBv7dt9Z1GKnZ/hw+D7sgSCFcH2vKPRT3AsRhVxVEZQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '21,': 0.07; 'attributes': 0.07; 'constructor': 0.07; 'dynamically': 0.07; 'objects,': 0.07; 'wednesday,': 0.07; 'wrapper': 0.07; 'cc:addr:python-list': 0.09; '"my': 0.09; 'coding,': 0.09; 'immutable': 0.09; 'mutable': 0.09; 'read-write': 0.09; 'java,': 0.15; 'thu,': 0.15; '2016': 0.16; '__slots__': 0.16; 'accidental': 0.16; 'attributes;': 0.16; 'class;': 0.16; 'consciously': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mean,': 0.16; 'object),': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'utterly': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'basically': 0.18; 'debugging': 0.18; 'detect': 0.18; 'integer': 0.18; 'python?': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'prevent': 0.20; 'candidates': 0.21; 'java': 0.22; 'lawrence': 0.22; 'am,': 0.23; 'code,': 0.23; 'leave': 0.23; 'bit': 0.23; '(you': 0.23; 'properties': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'message-id:@mail.gmail.com': 0.27; 'idea': 0.28; 'fine': 0.28; '"no': 0.29; 'about.': 0.29; 'editors': 0.29; 'code': 0.30; "i'd": 0.31; 'probably': 0.31; 'option': 0.31; "can't": 0.32; 'generally': 0.32; 'though,': 0.32; 'run': 0.33; 'common': 0.33; 'steven': 0.33; 'open': 0.33; 'editor': 0.34; 'handle': 0.34; 'running': 0.34; 'add': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'mix': 0.35; 'something': 0.35; 'level': 0.35; 'sometimes': 0.35; 'but': 0.36; 'needed': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'thanks': 0.37; 'wanted': 0.37; 'turned': 0.38; 'someone': 0.38; 'why': 0.39; 'does': 0.39; 'enough': 0.39; 'takes': 0.39; 'subject:-': 0.39; 'where': 0.40; 'some': 0.40; 'collection': 0.60; 'your': 0.60; 'information': 0.63; 'benefit': 0.66; '20,': 0.66; "they're": 0.66; 'virtually': 0.66; 'results': 0.66; 'talking': 0.67; 'day': 0.67; 'jul': 0.72; 'integrated': 0.72; 'chrisa': 0.84; "else's": 0.84; 'ever-growing': 0.84; 'forced': 0.84; 'leak.': 0.84; 'otten': 0.84; 'to:none': 0.91; 'leak': 0.91; 'subject:add': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc :content-transfer-encoding; bh=dOlao6te0xykoP2B7BhfFZkLhA3AQg9cs3/QorPrUug=; b=TtCySiJFz0LpVrZooFeBnb6qYT2ZoK0+6k9O0tTY21EI4Kj8eAv32O9+YLI/ZN1h1A pzasnnuLXf9xGaMupsNANCFU6z0KwaXDTshL5ljmGbL8NtJ0mfvNTg924e+cOj+e8pC4 gDckPfx83EYXBC/3LQPgeGDLV5+fdHeXglOoLLvMMxgRl8Hny2crrL2m2cfWyMjVfZ3c Q5QM2GvvgcFZLBFH4FNiiGGl0/obTuKOg/ef2YS7AtqmdjPlBuQUn4HEBk7f4mLWRk5k nJjMws385eF/IiPTcnftkmg22O8d2P8wYHSzCJjkt7wFcK95vMpao6+7nlgSEIf7WAcW 1jLw== 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:from:date :message-id:subject:cc:content-transfer-encoding; bh=dOlao6te0xykoP2B7BhfFZkLhA3AQg9cs3/QorPrUug=; b=dHv8XnvLSIDniFhx6temWatkd5WQlb7q1r7YmGkuzGPabnyJ7XI2Vw8Kuvz8HcSnGv 88yFcZHaLqBZVXx4cGHQ7zegYrDjrKvX0BYC5bs+Vf05ED9LAuU3MCyvttBiisYvi+xb 1XkVDgewTrKfhfj2aX8KDYcyy+KXl5XDBEvWuV6T7NacdW/sflK003G3Lxbj8gX5iF0x 2Vl/5z20c6NxMTilThBc08yQUBN3zbaqhcNhGx70ZU0lc8g/pr34YcSGoWQisDJtX7DI WNszGG3wMDKUUcHqP+NgcrbWd4QePWsEDsw7unjBgQ41hplEBjIepDyN34Zf5Oo/THGl FS9g== X-Gm-Message-State: ALyK8tLRc4UKSwsITpr8tbJDLpx54zAFKRa+X5pYYOViOWhe6BpkQkPrr9eYgJv91UVcevjR8uZ7V5/qKQThtg== X-Received: by 10.28.221.8 with SMTP id u8mr12411252wmg.61.1469068178966; Wed, 20 Jul 2016 19:29:38 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <4f796d95-0e87-4610-a0a8-1eff07c60ace@googlegroups.com> <578b9e62$0$22141$c3e8da3$5496439d@news.astraweb.com> <3e934f83-2076-407d-b85b-686ecdec6ff8@googlegroups.com> <2bc29306-ac90-4424-84c2-64be494c5a75@googlegroups.com> <230d1aff-b1a3-4f16-b9a6-b5d9fd07e4b4@googlegroups.com> <62dd86d4-74f7-4e97-a29c-7e45aced77d9@googlegroups.com> <18acf908-b761-4a60-8d7c-5e4576b03320@googlegroups.com> Xref: csiph.com comp.lang.python:111688 On Thu, Jul 21, 2016 at 8:11 AM, Lawrence D=E2=80=99Oliveiro wrote: > On Wednesday, July 20, 2016 at 9:16:30 PM UTC+12, Peter Otten wrote: >> >> Lawrence D=E2=80=99Oliveiro wrote: >> >>> On Wednesday, July 20, 2016 at 7:26:36 PM UTC+12, Peter Otten wrote: >>> >>>> pylint can detect candidates for accidental attribute creation: >>> >>> And __slots__ will prevent them outright. >> >> And attributes added intentionally. > > You mean, being able to dynamically add new attributes to an object? > > Probably not a good idea to mix that with read/write properties... This is exactly what Steven was talking about. If you can't handle dynamic read-write attributes (whether they're properties or simple attributes is orthogonal to this), why even use Python? You can get a linter to tell you about misspelled attribute names, which has a huge advantage over __slots__ in that it can give you the information prior to actually hitting that line of code (prior to even running the program - and some editors have integrated linters, so you don't even need to consciously run the linter, you just see the results as you type). That gives you virtually the same benefit as C/Java style of coding, where you're forced to declare all your members in the class; but as an added bonus, you can *dynamically add attributes*! Don't think that's useful? Well, the other day I wanted to add a wrapper around the constructor for someone else's object to stash a bit of extra information into it (to track down a resource leak - turned out there was an ever-growing collection of objects, because they weren't being removed from that object), and if I'd been using Java, the response would have been "no way does that even make sense". Thanks to dynamic code, I could do what I needed to and track down the leak. If the author of that object had used __slots__, though, I'd have been utterly stuck. Using __slots__ basically takes your object down to the level of a Java one. That's generally fine for something immutable (you really don't need to be adding attributes to the integer 42), but even with immutables, I've sometimes wanted to add hidden attributes; with mutable objects, it's common enough that you want to leave the option open unless you have good reason not to. And "my editor sucks" is not a good reason; nor is "my debugging skills suck". ChrisA