r/learnpython • u/MustaKotka • Nov 28 '25
Learning classes - ELI5 why this works?
class Greetings:
def __init__(self, mornin=True):
if mornin:
self.greeting = "nice day for fishin'!"
else:
def evening():
return "good evening"
self.__init__ = evening
print(Greetings().greeting)
print(Greetings(mornin=False).__init__())So this evaluates to:
nice day for fishin'!
good eveningI'm a bit unsure as to why this works. I know it looks like a meme but in addition to its humour value I'm actually and genuinely interested in understanding why this piece of code works "as intended".
I'm having trouble understanding why __init__() "loses" self as an argument and why suddenly it's "allowed to" return stuff in general. Is it just because I overwrote the default __init__() behaviour with another function that's not a method for the class? Somehow?
Thanks in advance! :)
60
u/feitao Nov 28 '25
ELI5: what kind of five-year olds would write such insane code?
23
5
u/csabinho Nov 28 '25
As long as it's just a proof of concept, it's kinda fine. But please don't use this in real-life code.
3
u/QultrosSanhattan Nov 28 '25
It's easy:
At case 1: you're defining __init__ as a bound method. Everything works as intended.
(Note that the bound method __init__ is triggered just before Greetings() is used)
At case 2: The bound method __init__ is executed, since mornin is False this time, you're replacing the bound method __init__ with a function, then you call the function __init__ which returns "good evening".
TL;DR: bound methods and functions aren't the same thing.
1
u/MustaKotka Nov 28 '25
But isn't
.__init__()supposed to return an empty object with no attributes? (By default.)2
u/deceze Nov 28 '25
No.
__new__(which you don't typically meddle with) is creating the object instance and returning it. It's merely executing that object's__init__method, so that object can initialise itself. Notice that it's called__init__, notconstructor similar.__init__doesn't return anything; or if it does, that return value simply vanishes into the void.2
2
u/Turtvaiz Nov 28 '25
Nope.
__init__()returns None. You might be thinking of__new__().>>> class Test: pass ... >>> type(Test().__init__()) <class 'NoneType'>1
1
u/Delicious_Egg_4035 Nov 28 '25
Sure, but any function that doesn't return "anything" returns None. I think that that is what he meant.
1
u/MustaKotka Nov 28 '25
class Greetings:
def __init__(self, mornin=True):
if mornin:
self.greeting = "nice day for fishin'!"
else:
def evening(_):
return "good evening"
Greetings.__new__ = evening
self.greeting = Greetings()
print(Greetings().greeting)
print(Greetings(mornin=False).greeting)Messed around with __new__ and came up with this cursed piece of code. The prints work as intended:
nice day for fishin'!
good eveningThank you everyone for your insights! I'm learning a lot and having tons of fun, too!
3
u/lekkerste_wiener Nov 28 '25
As others have pointed this code is really horrible Python.
Then again, you did say how memey it feels, so hey, kudos for exploring. :) this is something more beginners should do.
3
u/MustaKotka Nov 28 '25
Oh I know this is an absolute no-no!
But yeah, exploring is how I've learnt the most thus far.
11
7
u/MiniMages Nov 28 '25
This is a horrible example of a class and how to write and use one.
I learned python by going through freecodecamp and i'd recommend you go through the python classes section https://www.freecodecamp.org/learn/python-v9/lecture-classes-and-objects/how-do-classes-work-and-how-do-they-differ-from-objects
Can confirm this is an amazing online resource.
0
2
u/ottawadeveloper Nov 28 '25
When you make a method in a class, Python automatically wraps it so that it passes self as the first argument.
When morning=False, you're defining a new function with no self argument and setting init() to be that function. There's no magic wrapping here on Pythons behalf, so you end up with a normal function. Only functions declared directly under the class get the magic treatment to make them methods.
There's nothing special about Python functions or methods (even the magic ones like init()), they're basically all callable (ie you can use () after them to call them) properties of the instance object. You can overwrite them and mess with them however you want.
2
u/TheRNGuy Nov 30 '25
It's implicit argument in this method, or you'd have to add it in every class instance, it would make oop code look worse.
1
u/surreptitiouswalk Nov 28 '25
Why doesn't Greetings().__init__() raise a "too many input argument" error? Wouldn't class methods generally be called with self passed in as the first argument by default? Why does replacing it with a custom function override that behaviour?
1
u/enygma999 Nov 28 '25
Why would it?
Greetings()instantiates an instance of theGreetingsclass with itsmorninargument set to the default value ofTrue. That then has its__init__method called on it again, also with themorninargument set toTrueby default. So__init__would be called twice, both times withself, mornin=True.
13
u/deceze Nov 28 '25
That's exactly why. You're assigning a function to an attribute of the object. You later access this attribute, which gives you the function. You execute the function, which returns a string.
That this attribute you're assigning to and accessing later happens to be called
__init__is of no great consequence. You could name it anything else and it'd work the same. It happens to shadow the__init__class attribute; i.e. your object instance now has an attribute called__init__, and the classGreetingsdoes too, but they're not the same attribute.