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? > > > 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) > What the heck?? As in, why is he passing instance, cls and who is populating those vars? When you do f.name you just have self to pass into name.__whatever__(self) I haven't read the descriptor protocol as yet. > self.default = default if default else type() I don't understand how he's using type() like that or what it returns. Is it None? Why would type() return None when one can use that directly. -- https://mail.python.org/mailman/listinfo/python-list