why descriptors? (WAS: Make staticmethod objects callable?)

S

Steven Bethard

Steven said:
> (For anyone else out there reading who doesn't already know this,
> Steven D'Aprano's comments are easily explained by noting that the
> __get__ method of staticmethod objects returns functions, and classes
> always call the __get__ methods of descriptors when those descriptors
> are class attributes:
> Why all the indirection to implement something which is, conceptually,
> the same as an ordinary function?

While I wasn't around when descriptors and new-style classes were
introduced, my guess is that it's mainly because what you *usually* want
when defining a function in a class is for that function to be an
instance method. That is, the following code::

class C(object):
def foo(self):
pass
c = C()
c.foo()

should be much more common than::

class C(object):
def foo():
pass
C.foo()

because the whole point of creating a class is to allow you to create
instances. But if ``C.foo`` and ``c.foo`` are just regular functions,
then how will ``c.foo()`` get the ``self`` argument? Certainly a normal
``foo()`` shouldn't be inserting a magical ``self`` argument. So *some*
indirection has to happen when a function is used in a class.

Python's solution to this problem is to introduce descriptors, which are
the "something" that classes have to do. All classes invoke __get__
whenever any of their attributes are accessed. With a normal function
object, invoking __get__ turns it into an instance method:
.... pass
........ pass
........ def foo(self):
.... pass
....<bound method C.foo of <__main__.C object at 0x00E59C50>>

As a result, if you want to have a callable as a class attribute and you
don't want that callable to give you an instance method when you access
it, you can't use a regular Python function. Personally, I think that's
pretty reasonable since 99% of the time, I *do* want an instance method[1].

STeVe

[1] The other 1% of the time, I pretty much always want a classmethod.
I'm still convinced that staticmethods are basically silly when I can
just declare a module level function. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top