Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Descriptors vs Property Date: Fri, 11 Mar 2016 23:12:24 -0700 Lines: 70 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Mjre33nyPoPTZJKCowxPkQcicpF6A8JeCZ6SjK7non8g== 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; 'received:209.85.223': 0.03; 'value,': 0.03; 'essentially': 0.04; 'resulting': 0.04; 'attributes': 0.07; 'defines': 0.07; 'implements': 0.07; 'works.': 0.07; '@property': 0.09; 'attribute.': 0.09; 'descriptor': 0.09; 'skip:# 30': 0.09; 'example:': 0.10; 'skip:# 20': 0.13; 'syntax': 0.13; 'def': 0.13; 'instead.': 0.15; '"_"': 0.16; '10:59': 0.16; '2016': 0.16; 'descriptors.': 0.16; 'doing:': 0.16; 'foo()': 0.16; 'foo(object):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'there?': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'case.': 0.18; 'instance,': 0.18; 'skip:g 40': 0.18; 'otherwise,': 0.20; '%s"': 0.22; 'decorator': 0.22; 'parameter': 0.22; 'properties': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'skip:_ 20': 0.26; 'error': 0.27; 'checking': 0.27; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'skip:f 40': 0.27; 'this.': 0.28; 'arguments,': 0.29; 'follows': 0.29; 'implicitly': 0.29; 'methods.': 0.29; 'raise': 0.29; 'skip:s 30': 0.31; 'class': 0.33; 'skip:_ 30': 0.33; 'common': 0.33; 'foo': 0.33; 'this?': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'attempt': 0.35; 'instance': 0.35; 'protocol': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'received:209.85': 0.36; 'assigned': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'doing': 0.38; 'received:209': 0.38; 'delete': 0.38; 'why': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'called': 0.40; 'provide': 0.61; 'default': 0.61; 'more': 0.63; 'mar': 0.65; 'natural': 0.67; "'foo'": 0.84; 'to:name:python': 0.84; 'do:': 0.91; 'subject:Property': 0.91; 'thing,': 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=1tVdEV6VahEFZkKD6UNM5gnZnVgvwZzgaMFCWQtKBvY=; b=t2sb8RutzToCo6ZEZvDLseifMMV+75rvXrYXxocPikbFKTV9UhzlqaxVua9cHWAzVj mpim3VGDeAMuqvY2MTrKiXCnuFwYemHg26Eg+pA15Ukr395z+zmTAS3rtqq0/XjZls4P ayIikJzVmgQvtWD7YcKLDHmL1bFdb2BOeZWGIBbiv9yzzm7Yhr31YAz7VQE1T4WBdlG2 h3d5GcIHkE8XRENV5jhSOQvYZABjg6MFoUYdyhO0BX6+3jD7MamXpcXETHN1acrfJATU tXWmccPLWGVwk4ltQTWeJHKpF/d2MxhM3tRkdSMWDzezf4UGE1X+ryx8Y0ZE/kQMQgiO RlVw== 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=1tVdEV6VahEFZkKD6UNM5gnZnVgvwZzgaMFCWQtKBvY=; b=JqZ7G2ce5eiXTeXFcX0s1GKt+9qIiIoNYI0wzOpe7LMXls2i9k9NTkCEmE9dWPaeGJ ozLmscwofNJgrL3wjqnpuW3on23BRo30FNyDMx8Jb4jEt+dqrd2HIDVtVWEwYQ4lQelE PM/60FeiGCZN2MZcftf/KnCD2RdXA+i0dGJ8FQLcLhaP5ZVOvZl5YF0A6gN//4JeHxAK xNt+1k+4Dp+L6vSQcUOpozKYjlSyKBv0rn8nB1tUDBkqdyb15tLLZdSEkTdGWwjOP9fO pHAudaNZWm65c06uaf74b/45iZF/LfMOn0I3s4jKnmdbdcGhOibyazASk9Hs3pIT/dD4 Dz7A== X-Gm-Message-State: AD7BkJK/YOX+1VIU3Sn2vu0PRATVSivLiCnYWoMKFsfcQdFmNujHteVTV08PgcSRD9+wHG1efYMnKk6+I1DtNw== X-Received: by 10.107.11.148 with SMTP id 20mr5639965iol.111.1457763183717; Fri, 11 Mar 2016 22:13:03 -0800 (PST) In-Reply-To: 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:104673 On Fri, Mar 11, 2016 at 10:59 PM, Veek. M wrote: > A property uses the @property decorator and has @foo.setter > @foo.deleter. > > A descriptor follows the descriptor protocol and implements the __get__ > __set__ __delete__ methods. > > But they both do essentially the same thing, allow us to do: > foo = 10 > del foo > x = foo > > So why do we have two ways of doing this? Properties *are* descriptors. Properties just provide a more natural syntax for a very common case. > Also, > ##################### > class TypedProperty(object): > def __init__(self,name,type,default=None): > self.name = "_" + name > self.type = type > self.default = default if default else type() > > def __get__(self,instance,cls): > return getattr(instance,self.name,self.default) > > def __set__(self,instance,value): > if not isinstance(value,self.type): > raise TypeError("Must be a %s" % self.type) > setattr(instance,self.name,value) > > def __delete__(self,instance): > raise AttributeError("Can't delete attribute") > > class Foo(object): > name = TypedProperty("name",str) > num = TypedProperty("num",int,42) > > In this example, the class TypedProperty defines a descriptor where type > checking is > performed when the attribute is assigned and an error is produced if an > attempt is made > to delete the attribute. For example: > > f = Foo() > a = f.name # Implicitly calls Foo.name.__get__(f,Foo) > f.name = "Guido" # Calls Foo.name.__set__(f,"Guido") > del f.name # Calls Foo.name.__delete__(f) > ################################## > > I didn't follow this. Foo is a composition of TypedProperty. > You've got a 'Foo' type with two attributes 'name' and 'num'. > When you do f.name you are actually doing: > f.name.__get__(self, instance, cls) More accurately, you're doing f.__class__.__dict__['name'].__get__(self, instance, cls). But yes, this is how the descriptor protocol works. > What the heck?? > > I didn't follow this example at all.. What is he doing in there? > Also, what's this bit: > self.default = default if default else type() If the default parameter has a truthy value, it gets set to self.default. Otherwise, the type parameter is called with no arguments, and the resulting instance is used as self.default instead.