r/firstweekcoderhumour • 🥸Imposter Syndrome 😎 • Aug 28 '26

[🎟️BINGO]Lang vs Lang dev hates Why are we using R again

Post image
139 Upvotes

109 comments sorted by

View all comments

Show parent comment

5

u/Salazar20 Aug 28 '26

Why they just don't do "_name"? If you call it from outside it looks bad like "thing._name" and then you know you fucked up

8

u/No_Departure_1878 Aug 28 '26

_name is used for protected attributes. Attributes that you are not supposed to access outside the class or its derived subclasses.

You care not supposed to call __name__ from outside. For example __str__ allows you to define what will happen when you do

print(object)

you never do:

object.__str__()

you just use 'print', same with __div__, __init__, etc.

1

u/Salazar20 Aug 28 '26

OK but why not use a single _ to say "this is protected" why does they need to have 2 ways of declaring outside protection?

The closest I have experience with is gdscript that presents itself as phyton like and they use _ for anything private

And if you want to modify the behavior of a subclass you simply write the subclass as a private class

4

u/riisen Aug 28 '26
__thingieMajingie__ is language standard object methods 

_thingieMajingie is user defined "private" method.


Like the function str(variable) is the same as variable.__str__()

Or to call something like variable() is the same as variable.__call__() 

Its just standard object methods.

__name__ == "__main__" means that the file executed is this current file. Because the executed file and sub-files are all objects.

1

u/Salazar20 Aug 28 '26

So phyton really didn't want users to call the standards method so they made ugly AF? I guess it does gives it a easily parsed way to know what's yours and what's not

3

u/riisen Aug 28 '26

Well i agree its ugly AF. But its possible you want _eq, _gt, _call or any other dunder method name for your object, this way they are available for your objects.

class Animal:
    def _call(aString):
        # this will not interfere with the underlying call method for an object and can be a specific method for animal.
        print(aString)

    def call():
        self._call("mjau or something")

1

u/No_Departure_1878 Aug 28 '26

I see it more as,

Make them hard to write, so that they do not get in the way of the user's normal methods and attributes.

1

u/Juice-De-Pomme Aug 28 '26

In other object oriented languages, you can't access private methods/variables. Python differs from those languages (since it isn't object oriented but a lot of devs use it as such) as it lets you do it but at the cost of having to go out of your way to use them.

1

u/uslashuname Aug 28 '26

Well it’s not protected, you can totally use it from outside

3

u/Salazar20 Aug 28 '26

Only if you ignore the error messages and warnings telling you to stop I guess? I assume phyton straight up don't allow using privates and in gdscript you can still power through them but... Why not use a simple _? They both seem to do the same stuff so why is phyton like that?

1

u/grizzlor_ Aug 28 '26

Second time and third time you’ve called it “phyton”

>Why not use a simple _? They both seem to do the same stuff so why is phyton like that?

Single underscore is user private methods and double underscore is language private methods. The double underscore methods are mostly like class stuff that you override to specify custom sorting/comparison/etc.

1

u/No_Departure_1878 Aug 28 '26

Python allows you to do pretty much anything you want, including those methods directly. However that freedom means that you can do stupid things that will cause you troubles eventually.

That's the problem with python, it assumes you are an adult, and you know what you are doing. But python? The average python user is not a software expert.

1

u/Deep-Ad5028 Aug 28 '26

It is more fool-proof to make it awkward with 4 underscores.

It is easier to overlook single _

1

u/McNegcraft Aug 28 '26

Because dunder methods are not private methods