News for hackers. Stuff that matters.
Re:I'm Switching to Python and Actually Liking It by wiseowise on Wednesday July 16, 2025 @04:44AM (#44580071)
Bizarre is that you don’t consider it ugly.

Kotlin: constructor is either part of class definition or keyword constructor.

Ruby: initialize

JS: constructor

Python: ______new______, _______init_______

Literally this meme: https://knowyourmeme.com/memes/three-headed-dragon

Re:I'm Switching to Python and Actually Liking It by NoboruWataya on Wednesday July 16, 2025 @05:20AM (#44580293)
I like that magic method names generally all follow the same form so it's obvious that they are magic methods and not intended to be part of the public API. Whether that form uses double underscores or something else doesn't really matter to me as they are not being called directly.
Re:I'm Switching to Python and Actually Liking It by 63stack on Wednesday July 16, 2025 @06:00AM (#44580504)
Would you consider a constructor magic though?
Re:I'm Switching to Python and Actually Liking It by lou1306 on Wednesday July 16, 2025 @06:08AM (#44580538)
"Magic methods" is just the name people use for these special methods (another is dunder methods). They're "magic" because you do not call them by name, rather they are invoked by the interpreter in specific situations.
Re:I'm Switching to Python and Actually Liking It by dragonwriter on Wednesday July 16, 2025 @02:31PM (#44585425)
Yes, it is magic the same way other magic methods are, that is, because it is syntactically special and callable (and primarily called by) a syntax other than <instance>.<method>(...) or <class>.<method>(instance, ...)

Specifically, in the case of constructors, via <class>(...).

Re:I'm Switching to Python and Actually Liking It by fiedzia on Wednesday July 16, 2025 @08:50AM (#44581745)
> it's obvious that they are magic methods and not intended to be part of the public API

Is there an alternative API? No. This is public API regardless of anyone's intentions. Though "it's weird" is really not a very strong argument against it.

Re:I'm Switching to Python and Actually Liking It by trylist on Wednesday July 16, 2025 @11:11AM (#44583173)
Public API refers to the user of the class, not the implementer. You never call __init__ directly. __new__ allows modifying object creation itself for the implementer, but the user of the class will never call it.
Re:I'm Switching to Python and Actually Liking It by freehorse on Wednesday July 16, 2025 @11:36AM (#44583464)
There are private and public methods. Private methods are only supposed to be called within other methods, as in privately. Public methods are the ones that are normally called through the code, repl, by the user or whatever. You are not supposed to write `myclass.__add__(x)` anywhere except where you define the class itself and its methods.
Re:I'm Switching to Python and Actually Liking It by Izkata on Wednesday July 16, 2025 @12:36PM (#44584257)
There's actually 4 kinds:

  def foo(... # public
  def _foo(... # internal
  def __foo(... # munged
  def __foo__(... # magic
Internal is more convention as the language doesn't really do anything with it, but it does with munged, and magic methods are specifically for things implemented in the language.

Internal and munged don't exactly map to private and protected, but are kinda similar ish.

Re:I'm Switching to Python and Actually Liking It by freehorse on Wednesday July 16, 2025 @01:50PM (#44585040)
Oh right. I am not really into python, had read some doc about naming conventions some poitns.

In any case I actually like how one can use underscores to point on how exposed some method is supposed to be. Makes it simpler to actually know what to skip and what not.

Re:I'm Switching to Python and Actually Liking It by __MatrixMan__ on Wednesday July 16, 2025 @12:02PM (#44583783)
There are several alternative API's. @dataclass is one, Pydantic offers another, there's also attrs, plus less general things like namedtuple.

Admittedly it's obnoxious when you've got habits for one and you're on a team that uses another--totally flies in the face of the zen re: "there should be only one obvious way to do things".

...but that was always a rather ambitious goal anyway. I'm ok navigating the forest of alternative API's if it means not being locked into something that I can only change by choosing an entirely different language. I'm happy that it's very easy to tell when somebody is mucking about with python internals vs when they're mucking about with some library or other.

Re:I'm Switching to Python and Actually Liking It by yunwal on Wednesday July 16, 2025 @06:06PM (#44587318)
@dataclass does a very specific subset of what overriding dunder methods does. It’s not duplicating an abstraction it’s layering them
Re:I'm Switching to Python and Actually Liking It by t43562 on Wednesday July 16, 2025 @05:04AM (#44580187)
You don't call __init__ directly so the ugliness is limited to the definition which seems fairly minimal to me. It seems to be a naming convention for methods that are not invoked by name anywhere and that's at least informative when you're reading the class definition.

IMO this is less horrendous than e.g. go's insistence that exported functions are indicated by a capital letter - that really affects code using the module not just the definition.

Re:I'm Switching to Python and Actually Liking It by reedf1 on Wednesday July 16, 2025 @05:02AM (#44580177)
I had honestly not considered that the author might have been talking about the visual aesthetic of the dunder methods themselves. Honestly that has me a bit stumped; all I can say is that if you don't like underscores, don't pick python.
Re:I'm Switching to Python and Actually Liking It by dncornholio on Wednesday July 16, 2025 @05:58AM (#44580485)
I have found to believe that ugly and beautiful are irrelevant. As long as it's doing the job and not cause major headaches. Life is too short to worry about syntax.
Re:I'm Switching to Python and Actually Liking It by orphea on Wednesday July 16, 2025 @07:02AM (#44580835)
Right? I'm kind of surprised that a few underscores invoke such strong emotions in people.
Re:I'm Switching to Python and Actually Liking It by nottorp on Wednesday July 16, 2025 @12:43PM (#44584323)
> Life is too short to worry about syntax.

Every programming language you use will annoy you in some way. You stop caring after a few of them.

Re:I'm Switching to Python and Actually Liking It by timeon on Wednesday July 16, 2025 @06:12AM (#44580559)
Can you elaborate bit what is the problem here? Is it shape of the characters or something else?
Re:I'm Switching to Python and Actually Liking It by brabel on Wednesday July 16, 2025 @06:37AM (#44580684)
Amazing. If you need to ask this question I am guessing you have spent too long with Python . No other language uses this underscore madness for special methods or otherwise. Because it’s just , I don’t know a more appropriate word for it , stupid.
Re:I'm Switching to Python and Actually Liking It by Octoth0rpe on Wednesday July 16, 2025 @11:09AM (#44583147)
Oh yes they do: https://www.php.net/manual/en/language.oop5.magic.php

The programming languages world is broad/varied enough that any statement like "no other language does this!" is almost certainly wrong (outside of esoteric languages, which python and php most certainly are not)

Re:I'm Switching to Python and Actually Liking It by deathanatos on Wednesday July 16, 2025 @05:01PM (#44586731)
Lua, too:

  __add, __sub, __mul, __div, __mod, __pow, __unm, __idiv
  __band, __bor, __bxor, __bnot, __shl, __shr
  __concat, __len
  __eq, __lt, __le
  __index, __newindex, __call
  __gc, __close, __mode, __name
Re:I'm Switching to Python and Actually Liking It by Twirrim on Wednesday July 16, 2025 @11:47AM (#44583599)
What's actually amazing is how you're unable to answer a straight question.

If you call something "stupid" it doesn't really convey anything meaningful, especially not in the way you're using there, it comes across as "I don't actually have a reason I don't like it, I just don't".

Re:I'm Switching to Python and Actually Liking It by DrJosiah on Wednesday July 16, 2025 @02:06PM (#44585189)
I mean, Python is dynamic. Here, have this: https://gist.github.com/josiahcarlson/39ed816e80108093d585df...

Now you don't need to write your double-underscore methods ever again, if you don't want to.

Re:I'm Switching to Python and Actually Liking It by thesuperbigfrog on Wednesday July 16, 2025 @05:13PM (#44586841)
Most C and C++ compilers use similarly named macros:

https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macro...

Re:I'm Switching to Python and Actually Liking It by c6401 on Tuesday July 22, 2025 @11:20PM (#44655435)
JS used to have those, now they are deprecated though:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

so it settled on having this syntax for similar cases using "Symbol":

class Foo { *[Symbol.iterator]() { yield 1; yield 2; yield 3; } }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:I'm Switching to Python and Actually Liking It by dumah on Wednesday July 16, 2025 @10:20AM (#44582690)
I’m not sure why you’re comparing __new__ to constructors in other languages.

Ruby has the same thing but it’s called ‘new’.

Implementing the type of customization (idiomatically) that __new__ provides in Kotlin and JS isn’t any cleaner.

Re:I'm Switching to Python and Actually Liking It by parentheses on Wednesday July 16, 2025 @10:50AM (#44582975)
That's kind of the point. These methods are never meant to be called directly. They're used to desugar.

I think it's fairly short sighted to criticize these. FWIW, I also did that the first time I wrote Python. Other languages that do similar things provide a useful transparency.

Re:I'm Switching to Python and Actually Liking It by NewsaHackO on Wednesday July 16, 2025 @02:23PM (#44585351)
I don’t think anyone is criticizing its utility, just that the syntax of 2-5(?) underscores in a row isn’t something that is DX friendly.
Re:I'm Switching to Python and Actually Liking It by hiAndrewQuinn on Wednesday July 16, 2025 @02:30PM (#44585419)
The ugliness is intentional. Nobody wants to name their cool new variable __hot__singleton__in__your__area__.
Re:I'm Switching to Python and Actually Liking It by dragonwriter on Wednesday July 16, 2025 @02:27PM (#44585399)
The syntax is exactly two, not 2-5.
Re:I'm Switching to Python and Actually Liking It by JKCalhoun on Wednesday July 16, 2025 @04:09PM (#44586287)
I think the point was: who can know by looking at it.
Re:I'm Switching to Python and Actually Liking It by gcbirzan on Wednesday July 16, 2025 @04:24PM (#44586443)
I mean, maybe you should use a font that doesn't make it hard to figure that out. Next you're gonna say we should ban 0s and Os, Is and ls, because your font doesn't allow you to figure out which one it is.
Re:I'm Switching to Python and Actually Liking It by dragonwriter on Wednesday July 16, 2025 @05:39PM (#44587076)
Anyone using a monospacdd font, a font where underscores are separated, or a run-together proportional font that they are even a little bit familiar with, because of the relative width of underscores and other characters.

Which, between them, covers approximately everyone.

Re:I'm Switching to Python and Actually Liking It by globular-toast on Thursday July 17, 2025 @02:55AM (#44590496)
They are called dunder methods, meaning "double under". It's 2 underscores.

I've had snippets in my editor for approximately 15 years at this point so I don't have to manually type any of the dunder methods. Would recommend!

Re:I'm Switching to Python and Actually Liking It by __MatrixMan__ on Wednesday July 16, 2025 @12:09PM (#44583914)
It's like the bright orange garments that hunters wear, the ugliness is sort of the point. It says "this is a different sort of thing."
Re:I'm Switching to Python and Actually Liking It by ddejohn on Wednesday July 16, 2025 @01:35PM (#44584892)
> It's like the bright orange garments that hunters wear, the ugliness is sort of the point.

Ugliness is not the point of hi-vis vests lol, the point is to not get shot by other hunters.

Re:I'm Switching to Python and Actually Liking It by __MatrixMan__ on Wednesday July 16, 2025 @02:03PM (#44585164)
...by being as shockingly contrasting with their environment as possible. Maybe your aesthetics differ, but I find it quite ugly. If it wasn't, it wouldn't grab my attention so well. Wearing something shockingly ugly is a great way to not be mistaken for a deer.

By contrast, I find a well camouflaged deer quite beautiful--once I notice it at all. The beauty comes from the way that it is so clearly a thing of its surroundings. Nothing at all like a bright orange hat.

Re:I'm Switching to Python and Actually Liking It by ddejohn on Wednesday July 16, 2025 @02:17PM (#44585291)
> Wearing something shockingly ugly is a great way to not be mistaken for a deer.

Sure... yes the bright orange is ugly, but it's not the ugliness that prevents you from getting shot, it's the bright unnatural color. Other hunters aren't thinking "oh that's really ugly, it must not be something I can shoot" they're thinking "bright orange means person, I should not fire my rifle in that direction".

> If it wasn't, it wouldn't grab my attention so well.

Are you saying that if you thought the bright orange was pretty it wouldn't occur to you not to fire your gun in its direction?

Re:I'm Switching to Python and Actually Liking It by __MatrixMan__ on Wednesday July 16, 2025 @07:13PM (#44587860)
If I thought that bright orange was a part of a healthy forest ecosystem, I would likely see beauty in it. And if my intent was to shoot denizens of such an ecosystem, then yeah bright orange would make a poor indicator for "don't shoot here". You'd be better off with something ugly, something which clearly doesn't belong.
Re:I'm Switching to Python and Actually Liking It by guhcampos on Wednesday July 16, 2025 @12:24PM (#44584104)
Some-other-language-user complaining about python ugliness:_____new_____ and _____init_____

Same-other-language-user:

((()))()()()({}{}{{(((())){}}}}{}{}{};;;;();)(;}}}

*not supposed to be correct syntax, it's just a joke

Re:I'm Switching to Python and Actually Liking It by slightwinder on Wednesday July 16, 2025 @01:05PM (#44584559)
As if other language don't have their warts.. Ruby and Javascript for example have hideous syntax, comparing this with some special naming is quite bizarre.

But who cares? It's syntax, it has its purpose.

Re:I'm Switching to Python and Actually Liking It by moffkalast on Wednesday July 16, 2025 @01:38PM (#44584922)
I would think the dumber thing is having to create an empty file called __init__.py for a package to register as a package. And relative imports being completely fubar.
Re:I'm Switching to Python and Actually Liking It by kolanos on Wednesday July 16, 2025 @02:20PM (#44585321)
> I would think the dumber thing is having to create an empty file called __init__.py for a package to register as a package.

This hasn't been true since Python 3.3. You no longer need a __init__.py for Python to recognize a module, but it can still be useful in many cases.

Re:I'm Switching to Python and Actually Liking It by maleldil on Wednesday July 16, 2025 @04:34PM (#44586529)
This is incorrect.

> The __init__.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature).

https://docs.python.org/3/tutorial/modules.html

Re:I'm Switching to Python and Actually Liking It by mixmastamyk on Wednesday July 16, 2025 @02:00PM (#44585134)
The point is to keep them out of the object namespace, and works well for that.
Re:I'm Switching to Python and Actually Liking It by DrJosiah on Wednesday July 16, 2025 @02:04PM (#44585170)
I mean, it's never been a problem for me in my 26 years of Python, but if it is such a big deal for you, maybe you should consider this: https://gist.github.com/josiahcarlson/39ed816e80108093d585df...

I wrote it in about an hour and a half. It seems to work in Python 3.6 and 3.12. Now you never need to write another double-underscore magic method again.

Re:I'm Switching to Python and Actually Liking It by adw on Wednesday July 16, 2025 @02:19PM (#44585311)
Fewer characters than “initialize” or “constructor”, clearly marked as being “not a normal method”. Python is better here.
Re:I'm Switching to Python and Actually Liking It by dragonwriter on Wednesday July 16, 2025 @02:27PM (#44585389)
If you felt your argument about __new__ and __init__ was valid, you probably would have used the actual names and not hyperbolic exaggerations.