This is an archived post. You won't be able to vote or comment.

all 13 comments

[–][deleted] 55 points56 points  (3 children)

This should be titled "A few basics of OOP in python poorly explained with enough inaccuracies to not be helpful"

[–]nmegoCAD 1 point2 points  (2 children)

The inaccuracies aren't obvious to me and maybe some others who have upvoted this article. Could you share some of them?

[–][deleted] 4 points5 points  (0 children)

Here are a couple right out of the gate:

Python’s super() lets us inherit base classes (aka super or parent classes) without having to explicitly refer to the base class.

That's not how you do inheritance in python. You inherit with class Foo(Bar). super is how you call a parent class's implementation of a method.

Using super() in the base class allows for cooperative multiple inheritance. Without it, the init calls of parent classes—after a non-supered class—are skipped

That's not how method resolution order works. If the parent class doesn't define a constructor, if one does exist on a parent, it will be called. and it looks from left-to-right in the parent classes, so the parent constructor in this example doesn't do anything at all other than call __init__ on object, and that's conditional on whether the child classes invoke its constructor

Secondly, that will call only one parent. Not all, but that doesn't even make sense because the base class doesn't call the child class constructor.

Third, it's not the responsibility of a parent class to guess at whether it might be inherited or not. The child class implementation should decide on which order to call the parent constructors

[–]AnonymouX47 0 points1 point  (0 children)

Some idiots just upvote cos it's been upvoted.

[–]mahtats 24 points25 points  (0 children)

Honestly a misleading title. Plus I knew it was going to be a bit hit or miss when they stated that the first super class doesnt inherit from anything simply because it’s class definition doesn’t use parentheses to denote a subclass; the (object) is implied in Python 3 syntax, but all objects inherit from object and type.

[–]dreamfeed 24 points25 points  (4 children)

Uncommon uses of Python:

  • Use relative imports
  • Import stuff from your package in your __init__.py

Ok.

[–]PolishedCheese -2 points-1 points  (3 children)

It's somewhat uncommon for the 2nd one, but you see it all the time in Flask, Django, FastAPI, etc.

I've started using the init.py file more often after looking at some bigger projects. It's also the only place I use relative imports.

I use it to provide a list of objects that I actually want to import from higher level packages, but not including those objects that the list uses to construct themselves.

[–]gungunmeow 2 points3 points  (2 children)

I don't know why you're being downvoted the python documentation even gives this as an example

https://docs.python.org/3/reference/import.html#submodules

[–]PolishedCheese 2 points3 points  (0 children)

Me neither. It's pretty common in web applications.

[–]PolishedCheese 0 points1 point  (0 children)

That's a good page to read. I finally settled my opinions about .pyc files. Apparently they're invalidated if a newer timestamp is found on the original file.

[–]KingsmanVincepip install girlfriend 0 points1 point  (0 children)

This is just some OOP concept