2

I'm definitely still in the Python learning phase, but I've learned a little about the Python dunder methods (__init__, __cmp__, __add__, etc.) and as best I can tell, these are ways that the core Python developers to allow programmers to implement special methods for their own classes.

So, it seems like those ought to be well-defined and limited to whatever the core developers specify. But today (as I'm starting to learn SQLAlchemy), I ran across their own attributes like __tablename__ and __table_args__

To me, that's confusing. I thought the Pythonic meaning of dunder methods or attributes implied that they were part of the core of Python. But SQLAlchemy appears to be using it as a convention to hide lower-level parts of their ORM.

Is this good practice? If I'm writing classes, should I feel free (or even be encouraged) to make up my own dunder attributes or methods?

George Adams
  • 458
  • 4
  • 15
  • 2
    http://stackoverflow.com/questions/21587882/how-recommended-is-using-custom-double-underscore-variables-in-python This one is probably more relevant. Best practice is often times ignored for libraries that are specialized for certain things. [`zope.interface`](https://pypi.python.org/pypi/zope.interface/4.1.3) is another library that does this. – metatoaster Nov 26 '15 at 03:28
  • 2
    Thanks, that's helpful. It seems like one of the main dangers is that the core developers might one day decide to implement their own dunder method that will conflict with some package developer's own. Admittedly this is probably unlikely for, say, __tablename__, but it still seems like SQLAlchemy is doing something that PEP8 strongly discourages... – George Adams Nov 26 '15 at 03:44

0 Answers0