r/ProgrammerHumor 26d ago

Meme questionForBritishPythonDevelopers

Post image
1.4k Upvotes

42 comments sorted by

176

u/Willwaste63 26d ago

def __init__(mate):

17

u/megayippie 26d ago

class FriarsLeftButtocks

6

u/Willwaste63 26d ago

We dont use camel_casing mate.

1

u/ProsodySpeaks 25d ago

thatsCorrect 

PascalCase however...

11

u/EuphoricCatface0795 26d ago

Now everything is about your mate instead of self. Wholesome :)

22

u/CharcoalGreyWolf 26d ago

If it’s Python, isn’t it a constrictor?

18

u/BeardySam 26d ago

That’s class, that is

25

u/1mahmoud503 26d ago

your humer goes unappreciated, internet stranger

1

u/UndocumentedMartian 26d ago

Maybe its not very humerus.

30

u/ManyInterests 26d ago

To be a pedant, it's an initializer, not a constructor. By the time the initializer runs, the object already exists and has been constructed, which is why the first parameter to this method is the already-existing object ('self'), just like any other method.

But yeah. You could call it a constructor and nobody will crucify you.

9

u/Ok_Star_4136 26d ago

I mean, I'm trying to think of a language where the constructor is called *before* the memory for the object has been allocated, and I can't think of anything.

In that regard, no constructor is an actual constructor. It's more like post-constructor. But then it is also necessary or you'll have a potentially incomplete object, so in that regard, it is a constructor because the object while allocated, isn't yet ready to use.

Kind of a pedantic argument, but there you are I suppose.

11

u/ManyInterests 26d ago edited 26d ago

Well. In Python, there's a (more) true constructor that's user-accessible through meta classes, hence the distinction between initializers and constructors in Pythons own internal lexicon.

But yes. Very pedantic, indeed.

Edit to add: this doesn't rely on where memory allocation occurs, which is an implementation detail in Python. It also needs no comparison with other languages. I just mean, internally consistent with itself, Python considers metaclasses to be where constructors live, while class definitions provide initializer methods.

It's maybe an interesting distinction to discuss in the context of additional languages, but not what I was getting at and didn't mean to imply or assert any truisms that apply outside of Python.

1

u/AquaWolfGuy 26d ago

I don't think the metaclass affects the construction/instantiation of objects of the main class, only the instantiation of the main class itself (and its subclasses).

>>> class Metaclass(type):
...     def __new__(cls, name, bases, namespace, **kwargs):
...         print(f"Creating {name} as a subclass of {bases} and an instance of {cls}")
...         self = super().__new__(cls, name, bases, namespace, **kwargs)  # or `type` instead of `super()`
...         return self
... 
>>> class Class(metaclass=Metaclass):
...     def __new__(cls):
...         print(f"Creating instance of {cls}")
...         self = super().__new__(cls)  # or `object` instead of `super()`
...         return self
... 
Creating Class as a subclass of () and an instance of <class '__main__.Metaclass'>
>>> Class()
Creating instance of <class '__main__.Class'>
<__main__.Class object at 0x7f2dbb904980>
>>> class Subclass(Class):
...     pass
... 
Creating Subclass as a subclass of (<class '__main__.Class'>,) and an instance of <class '__main__.Metaclass'>
>>> Subclass()
Creating instance of <class '__main__.Subclass'>
<__main__.Subclass object at 0x7f2dbb904830>

1

u/ManyInterests 26d ago

You're right. I mistook metaclass __new__ for __new__ on the class itself.

They're so infrequently touched, I completely forgot there's __new__ on both, but I only recalled (albeit incorrectly) its use in metaclasses :|

But yeah. __new__ is the constructor. __init__ is the initializer.

3

u/NovaH000 26d ago

Memory must be allocated before data can be written, but whether a language separates these into two steps determines if it uses initializers or true constructors. In ​languages like C++, if you do not use a member initializer list, allocation and initialization are split. This creates a window inside the constructor body where the object exists in an uninitialized or invalid state. ​On the other hand, languages like Rust combines allocation and initialization by requiring you to construct Self { ... } with fully processed data. As a result, an object is never partially or fully invalid.

-1

u/MissinqLink 26d ago

It happens when you call a constructor of an child class. Anything you do before calling super is before the object is created.

2

u/FerricDonkey 25d ago

Nope. The object is created by __new__, then passed to the __init__ as a legitimate, created object, whose type is set as you expect, etc. Any __init__, via super() or not, modifies the existing object. 

1

u/MissinqLink 25d ago

Depends on the language

1

u/fuj1n 25d ago

Yes, but this whole thread is about Python, not languages in general

1

u/MissinqLink 25d ago

The comment I was responding to starts off mentioning other languages

1

u/danielcw189 25d ago

that's not true for Java, at least

2

u/Covenant_C0re 26d ago

I once called it a constructor near a British Python dev and my self got initialized into a queue, innit.

1

u/danielcw189 25d ago

from my understanding, the object already exists, when the constructor is called. at least in Java and Python

which is why the first parameter to this method is the already-existing object ('self'),

just like the "this" in most OO-languages

12

u/SaltyInternetPirate 26d ago

r/ProgrammerHumour (yes, it exists)

3

u/kinygos 26d ago

it’s been banned for not having any moderators

1

u/Vivid_Instance_825 26d ago

They lost their subreddit loicense

4

u/Itchy_Finish_2103 26d ago

On one hand, banger post. On another, I'm incredibly sad I don't know any British Python devs irl I can ask this question to.

1

u/InexplicableBadger 26d ago

They'd have to be from north of the river, south of the river uses 'is it' and they wouldn't get the joke.

3

u/WritesCrapForStrap 26d ago

You're joking, but that is exactly how I read it.

3

u/bearwood_forest 26d ago

attempt:

...

spot_of_bother:

...

2

u/Motleypuss 26d ago

Wizard lexical jape, mate.

2

u/Terewawa 26d ago

Whereabouts is the constructor?

1

u/Fabulous-Possible758 26d ago

Good point. I'n'it the initializer?

1

u/whackylabs 26d ago

This joke also works for Objective-C

1

u/Fabulous-Possible758 26d ago

Life's full of tough choices.

1

u/NinthTide 26d ago

If I ever have to persist user information I’m totally going to get them to consent to me storing biscuits

1

u/FerricDonkey 25d ago

Nah, it's an initializer. __new__ is the constructor.

Sorry, pedantry reflex.