473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Steven Bethard wrote:
(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:
Steven D'Aprano wrote: 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:
class C(object): .... pass
.... def foo(self): .... pass
.... foo <function foo at 0x00E69530> foo.__get__(C(), C) <bound method C.foo of <__main__.C object at 0x00E738F0>> class C(object): .... def foo(self):
.... pass
.... C().foo

<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. ;)
Mar 1 '06 #1
0 1286

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Denis S. Otkidach | last post by:
I've noticed that the order of attribute lookup is inconsistent when descriptor is used. property instance takes precedence of instance attributes: >>> class A(object): .... def...
4
by: Michal Vitecek | last post by:
hello everyone, today i've come upon a strange exception, consider the following file test.py: --- beginning of test.py --- class A(object): def method1(parA): print "in A.method1()"
68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
0
by: Robin Becker | last post by:
A colleague wanted to initialize his class __new__ and tried code resembling this #######################1 class Metaclass (type): def __init__(cls, name, bases, *args, **kwargs):...
11
by: cjl | last post by:
Hey all: I want to convert strings (ex. '3', '32') to strings with left padded zeroes (ex. '003', '032'), so I tried this: string1 = '32' string2 = "%03s" % (string1) print string2 >32
10
by: Nicolas Fleury | last post by:
Hi everyone, I was wondering if it would make sense to make staticmethod objects callable, so that the following code would work: class A: @staticmethod def foo(): pass bar = foo() I...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
8
by: M.Endraszka | last post by:
Hi, I am writing a simple irc-like server and came across weird behavior while debugging. Could anyone tell me why the following happens : I have a class which stores pipe descriptors as a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.