logoalt Hacker News

pjmlpyesterday at 5:08 PM4 repliesview on HN

Since Python introduced new style classes, it also became a pure OOP language, even though it might not look like it at "Hello World" level, all primitive types have become objects as well.

I love to point this out to OOP haters,

    >>> type(42)
    <class 'int'>

    >>> dir(42)
    ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'is_integer', 'numerator', 'real', 'to_bytes']

Replies

aidenn0today at 2:01 AM

If that's enough to make a language pure OOP, then Common Lisp is also a pure OOP languge:

  CL-USER> (class-of 42)
  #<BUILT-IN-CLASS COMMON-LISP:FIXNUM>
the__alchemistyesterday at 11:32 PM

I have found the definition of OOP to be fuzzy. For example, I don't see why having methods would make a data type object oriented. I associate OOP with factories, inheritance, using classes in places that might be functions otherwise, and similar abstractions.

Perhaps this is the counterfactual: I program in Python regularly, but don't program in an OOP style; I use dataclasses and enums as the basis, in a way similar to Rust, which by some definitions can't do OOP. So, if Rust can't do OOP (assumption) and I can write Python and Rust with equivalent structure (Assumption), does that mean Python isn't strictly OOP?

show 1 reply
ang_cireyesterday at 5:18 PM

This is very cool, and I did not know this. Thank you!

I wonder if my formal university python training predated this change (~2010), or if the professors were themselves unaware of this.

show 1 reply
arijunyesterday at 5:53 PM

> I love to point this out to OOP haters

That seems like a pretty lame gotcha--saying "Aha! The language you write in uses your hated paradigm under the hood" seems to invite the immediate response of "So? I don't use it."

show 1 reply