Python's Class Development Toolkit

Next Day Video · 252K views · 2013 · 45:56

Open on YouTube

Python, Ruby & scripting languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about alternate constructors with @classmethod, static versus class methods, and what double-underscore name mangling is really for.

Recommended by 17 people across 18 different months, 2012 to 2022.

What it is

Raymond Hettinger's PyCon 2013 talk is a 46-minute tutorial on Python's built-in tools for building classes. It uses common design problems to show why features like @classmethod, @staticmethod, properties and double-underscore name mangling exist and how to use them.

What people said

Strong rec nutship · 2014-02 · Stack Overflow · on “Creating a Python classmethod”

I've familiarized myself with the concept, most notably by watching Raymond Hettinger's excellent video [this video] and reading the accepted answer here [LINK] and I am wondering what I got wrong. class ReadHTML(object): def __init__(self, url): page = urlopen(url).read() self.page = page @classmethod def from_file(cls, path): page = open(path).read() return cls(page) This works r = ReadHTML(' [LINK] ') print r.page and this is not r = ReadHTML.from_file('example.html') print r.page it throws me an error, as if I was trying to "urlopen" a file: File "/usr/lib/python2.7/urllib2.py", line 258, in get_type raise ValueError, "unknown url type: %s" % self.__original ValueError: unknown url type: <!do

Strong rec Lukas Graf · 2015-07 · Stack Overflow · on “Parent accessing class variable defined in child in Python?”

@Pippi think of it this way: `self` always means "an instance of myself *or my super- or subclasses*" - so yes, methods on the parent class do have access to instance members (attributes or methods) of its subclasses. I recommend Raymond Hettinger's talk Python's Class Development Toolkit [this video] on this topic.

Strong rec sytech · 2016-10 · Stack Overflow · on “Python property and Python property setters type modified”

Raymond Hettinger Python's Class Development Toolkit [this video] This would be invaluable in learning the Pythonic way of developing classes. Raymond H is also an excellent authority for all Python topics. Read/watch his stuff whenever you can.

Strong rec Kevin J. Chase · 2017-03 · Stack Overflow · on “When to know to convert a method into a static method”

I highly recommend Raymond Hettinger's 46-minute PyCon 2013 talk, " Python's Class Development Toolkit [this video] ". He's one of Python's core developers, and his lectures are always fun as well as informative. This one demonstrates the "whys" behind many features of Python's classes, including `@staticmethod` and `@classmethod`.

Strong rec Rick · 2017-05 · Stack Overflow · on “Using value overriden by child in a parent function”

As a followup to the other answer, I highly recommend watching this talk [this video] from PyCon 2013, in which the purpose and use of the Python name-mangling paradigm is discussed. To summarize, using double underscore for your object variables it not a way to keep them "private". In fact, private variables in Python are not "a thing"; when coming from other languages, using Python effectively requires a shift in thinking on this and other topics. The purpose of name-mangling it to prevent parent and child classes from falling into the trap of namespace wars, in which, for example, some child class is using an attribute name for a different purpose than the parent class. For further reading, se

Strong rec Karl Knechtel · 2020-08 · Stack Overflow · on “Can a parent's class method call an abstract method which will be implemented in a child class?”

Here is a working version: from abc import ABC, abstractmethod class Parent(ABC): @staticmethod # not needed, but is documentation @abstractmethod def a(): pass @classmethod def b(cls): return cls.a() + 1 class Child(Parent): @staticmethod def a(): return 3 We can test it: >>> c = Child() >>> c.b() 4 Noteworthy things: We need to use the ABC base class (or the ABCMeta metaclass) in order to have access to the abstractmethod decorator, as explained in the abc module documentation. __class__ is not a keyword; it is an attribute of objects . We cannot just do things like __class__.a() because there is nothing to get the __class__ from . We could address this using the self parameter of an ordin

Recommends alonisser · 2013-11 · Stack Overflow · on “Is there a way to run a method for all instances of a class in python?”

I think all answers are valid, depending on the use case. I would argue that if this class is used in seperated parts of your app/program you wouldn't want to depend on the GLOBAL_MYCLASS_LIST declared some where before (as @amber suggested). You would want to be sure this variable exists and declare it as a Class Variable [LINK] and have the compute_all as a Class method or even as a static method [LINK] with @staticmethod decorator (which I think is less relevant to the specific use case. Again, I would stress that this is a very specific use case, and not very common. so you should think about your design before doing that and not using the more straight pythonic ways offered in other ans

Recommends johncip · 2014-05 · Stack Overflow · on “Why do some functions have underscores "__" before and after the function name?”

Raymond also explains why you'd want the name mangling behavior starting at around 34 minutes into this video: [this video]

Recommends Stewart Smith · 2016-03 · Stack Overflow · on “Best practice to implement multiple constructors in python”

UPDATE: The more pythonic way of doing multiple constructors is with @classmethod [LINK] , as suggested by Jim [LINK] . Raymond Hettinger did a talk on Python's class development toolkit at Pycon 2013, where he talked about multiple constructors [this video] using @classmethod . class Line: def __init__(self, a, b, noSlope): self.a = a self.b = b self.noSlope = noSlope @classmethod def fromPoints(cls, point1, point2): deltaX = point2[0] - point1[0] deltaY = point2[1] - point1[1] if deltaX == 0: return cls(point1[0], 0, True) else: a = deltaY / deltaX b = point1[1] - a * point1[0] return cls(a, b, False) @classmethod def fromVector(cls, vector, point): if vector[0] == 0: return cls(point1[0], 0, T

Recommends Arya McCarthy · 2017-06 · Stack Overflow · on “Multiple constructors: the Pythonic way?”

Most Pythonic would be what the Python standard library already does. Core developer Raymond Hettinger (the collections guy) gave a talk on this [this video] , plus general guidelines for how to write classes. Use separate, class-level functions to initialize instances, like how dict.fromkeys() isn't the class initializer but still returns an instance of dict . This allows you to be flexible toward the arguments you need without changing method signatures as requirements change.