Troskyvs
Troskyvs

Reputation: 8047

Python's descriptor "__get__" was not called as I expected, reason?

I was trying python's descriptor of get, to see if it's called.

I've got the following:

"""This is the help document"""
class c1(object):
    """This is my __doc__"""
    def __get__(s,inst,owner):
      print "__get__"

    def __init__(s):
      print "__init__"
      s.name='abc'

class d(object):
    def __init__(s):
    s.c=c1()

d1=d()
d1.c
print d1.c.name

I expect that it will call get function. But it fact the output is

__init__
abc

Why my "get" function was not called by instance owner of "d1"?

Thanks!

Upvotes: 0

Views: 46

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Descriptors must be bound to a class, not an instance.

class d(object):
  c = c1()

Upvotes: 1

Related Questions