Beazley 4e P.E.R pg 127: Descriptors?
<pre><code>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)

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)
</code></pre>

<p>I understand that he is creating an object called a TypedProperty to
manage attributes - in other words a decriptor.
But why does he do: </p>

<blockquote>
  <p>self.name = "_" + name</p>
</blockquote>

<p>If his purpose is name-mangling then it should be __ and this isn't a
module lvl statement so it can't be to hide from import.
Also why is self.default = ... outindented?? shouldn't it be part of the
def??</p>

<p>Also, do <strong>get</strong> etc work only with descriptor objects?? or can you use
them with 'properties' too? Properties and descriptors sound awfully
alike so I was wondering?</p>

<p>I didn't wuite follow this example so any explanation would help.</p>
