Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Lost in descriptor land Date: Thu, 30 Jun 2016 19:36:13 -0600 Lines: 57 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de XQA6+vDKpQ85lQ9GgmoZeQn27JWDImS6MHjiKXbiNn5Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'classes.': 0.07; 'correct.': 0.07; 'descriptor': 0.09; 'instance.': 0.09; 'none.': 0.09; 'objects.': 0.09; 'example:': 0.10; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'thu,': 0.15; '2016': 0.16; 'called,': 0.16; 'comments:': 0.16; 'descriptor,': 0.16; 'descriptors': 0.16; 'descriptors.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'temp': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'implementing': 0.18; 'instance,': 0.18; 'of.': 0.18; 'refers': 0.18; 'say,': 0.18; "shouldn't": 0.18; 'variable': 0.18; 'all,': 0.20; 'fairly': 0.22; 'class,': 0.22; 'properties': 0.24; 'header :In-Reply-To:1': 0.24; 'all.': 0.24; 'discussion': 0.24; 'parameters': 0.27; 'message-id:@mail.gmail.com': 0.27; 'idea': 0.28; 'declared': 0.29; 'invoke': 0.29; 'motivation': 0.29; 'protocol.': 0.29; 'objects': 0.29; 'tutorial': 0.29; 'normally': 0.30; 'implement': 0.32; 'point': 0.33; 'class': 0.33; 'url:python': 0.33; 'gets': 0.35; 'received:google.com': 0.35; 'behind': 0.35; 'clear': 0.35; 'instance': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'method': 0.37; 'setting': 0.37; 'itself': 0.38; "won't": 0.38; 'self': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'hello,': 0.40; 'some': 0.40; 'url:3': 0.60; 'your': 0.60; 'share': 0.61; 'advanced': 0.61; 'per': 0.62; 'course': 0.62; '30,': 0.63; 'believe': 0.66; 'accessed': 0.66; 'are?': 0.84; 'concept.': 0.84; 'self.value': 0.84; 'temperature': 0.84; 'to:name:python': 0.84; 'comment.': 0.91; 'write:': 0.91; 'overall,': 0.93 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:to; bh=0WUcT72I01HDoIZ/yMRAiaKl+EvSvFEBmbylQBo4QTY=; b=V+R0q6l7JgbA1weF4IMvAlNPzQ7IiLZVLqh1T+NSLFYCa8G0ln0os/sm10AfG+oSzc 0M1m8ttjSsG0j9RpyetvfZcUa4Byrplron6jgfl2IgfrkQN5jR8P85OyQ7IdFc0j3xxV nDTjPh1Byk2OBGWdd8viU4bixDhtadri4E2V6tNGIvqRTgl8VjFAZLGTbIIfSj5xBOoT R6TSDMUh9JW9fN8R7U0kaBsZ0REgqh3Ri2df5XF4glJ3j+n5YwOn/7WGqmo9mpOi0NWx 4UTa36UAacRtHVahioopgmDg2ygSyWWF7gAjv7Snw6N/UkvpLs+hytdZKbyvyrewTDeN /fAw== 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:to; bh=0WUcT72I01HDoIZ/yMRAiaKl+EvSvFEBmbylQBo4QTY=; b=Wh8uFQ1j3TRq959qh2T5TGfoPtM+fqhLSvxPk1YH3OHA12mCWqR4aKPH4Kon1zihv9 z5s8wbZ0+qMRN6MGmrlU3mLh/iKEDjWY1U0uCdvfEKlnxMqxCYnTwK5X6PwI7XwFL41s G/iC9Z+kfGh8v6d8n+ggY9I2S9S8nn9P6u/f74xk2UIDkOu6ao0sW7+yyW4w7v80043G +k0LmNnRdTCrfWhewRl7Ivman06hzmHzjzGwwatWiOoLD6ubsWXjBxiPQQeBddTy05jj gOD+6NvYEgYsiql//y5cXw0vx6Rs05vFIa9iCunEHbMV7XPIAEyJOhKDT0XWKDgQk+DU ufWw== X-Gm-Message-State: ALyK8tJ+SIPimatuHsJbsycp3BdrOFndCe0pm1uuDXFbpUtc86pMarJFWueGq1trHzFXAiNDgPLJYpvEP/aH7g== X-Received: by 10.202.80.78 with SMTP id e75mr10584492oib.91.1467337013035; Thu, 30 Jun 2016 18:36:53 -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: Xref: csiph.com comp.lang.python:110872 On Thu, Jun 30, 2016 at 7:06 PM, Ankush Thakur wrote: > Hello, > > There's something I don't understand about descriptors. On a StackOverflow discussion (http://stackoverflow.com/questions/12846116/python-descriptor-vs-property) one of the answers provides the following descriptors example: > > class Celsius( object ): > def __init__( self, value=0.0 ): > self.value= float(value) > def __get__( self, instance, owner ): > return self.value > def __set__( self, instance, value ): > self.value= float(value) > > class Temperature( object ): > celsius= Celsius() > farenheit= Farenheit() > > ... and then gets chided in the comments: > > "I believe your descriptor implementation of celsius is not correct. You should have set the celsius on instance rather than self; If you create two Temperature objects they will share the same celsius value." That criticism is accurate. "self" above refers to the Celsius instance, and there is only one Celsius instance for the Temperature class, not one per Temperature instance. > Overall, I have two problems: > 1) I don't get the idea behind the 'instance' and 'owner' parameters at all. Is there some simple tutorial that can explain these? First of all, do you understand what descriptors are? This is a fairly advanced Python concept. For a general tutorial, I would point you to https://docs.python.org/3/howto/descriptor.html Whereas the self argument refers to the Celsius instance, the instance argument refers to the instance of the object currently being accessed through the descriptor protocol. That is, the object that the Celsius descriptor is implementing a property of. Using the above example, if I were to write: temp = Temperature() print(temp.celsius) The access of temp.celsius will cause Celsius.__get__ to be called, and the value of instance for that call will be temp The owner argument is the class that this Celsius instance is a descriptor of. Normally that is just type(instance); however it is possible to invoke the __get__ method on the class object itself rather than on an instance, in which case owner is (still) the class object but instance is None. > 2) I don't understand the motivation behind the comment. Of course declaring a class variable would cause celcius to be the same for all objects. Shouldn't we be instead using self.celcius in, say, __init__() and then everything will work fine? It's not clear to me what alternative you're proposing. The reason celsius is declared on the class is because it's a descriptor, and descriptors implement properties of classes. Setting an instance attribute to a descriptor won't accomplish anything.