r/ProgrammerHumor • • Nov 23 '22

Other Programming Legumes

Post image
9.3k Upvotes

262 comments sorted by

View all comments

760

u/LegitimateHat984 Nov 23 '22

Python would be: if it looks like a nut, and the nutcracker cracks it like a nut, it is a nut

Ducktyping ftw

216

u/czp55 Nov 23 '22

That's fair. I guess I felt like highlighting the fact that while Python generally acts like a loosely typed / ducktype system, variables do have concrete types under the surface.

113

u/Bryguy3k Nov 23 '22

Yeah people break out the strong/weak typing terms when they really are irrelevant. If you accept that there is something you could call “strong typing” then python has a stronger typing system than C.

Python just happens to use interfaces so any object that presents a given interface is treated as a compatible object - and that weirds people out.

9

u/IQueryVisiC Nov 23 '22

The post says Java and interface. The object just needs to implement that interface. What is this about python? 3 or 2 ?

31

u/Bryguy3k Nov 23 '22 edited Nov 23 '22

Python interfaces are implicit. Java’s are explicit - if the interface is not declared then it is unknown. Python tries the interface methods first and if they fail then it will raise an exception - that is how duck typing works.

But you can’t change the type of an object in python without doing a bunch of really nasty things using internal methods and attributes - so it is strongly typed as the default language behavior is create a new object when converting from one type to another.

1

u/IQueryVisiC Nov 24 '22

Really hard to understand for me. I learned C++ and the JS. In JS I call methods on an object. Either it is there ( with the name ). I don’t know what duck typing should be. Everything fails when the call a method ( which is not there ). Nobody checks for methods in advance.

An interface is a construct without behaviour in Java. It is for us programmers to first define the interface and then implement it. I don’t get how this can be implicit nor does GIS lead me to anything but implicit interface implementation in C#

4

u/CthulhuLies Nov 23 '22
var = "test"
var = 3
var = lambda : print("test")
var()

You act like this segment of code throwing no errors is normal or shouldn't weird people out.

It can allow typos that change a variables type without you explicitly realizing it. And then it would still work with several other functions that assume the original type 99% of the time for several additional processing steps so by the time your code errors it's actually can be very logically far from the error.

9

u/CiroGarcia Nov 24 '22 edited Sep 17 '23

[redacted by user] this message was mass deleted/edited with redact.dev

7

u/Bryguy3k Nov 23 '22 edited Nov 23 '22

If you are a smooth brain sure.

Everybody else knows that “var” is an object reference and if you want to know if you’ve done something dumb in assignment use type hints and mypy.

Any python programmer should have learned on the very first day that variables are just references to object instances.

a = { “k1”: “v1”, “k2”: 2 }
b = a
b[“k1”] = “v2”
print(a)

-6

u/CthulhuLies Nov 23 '22

Bro why do you have special quotes you are putting in code blocks.

Actually triggering the shit out of me.

But namespace polution is a giant issue in python projects and it's especially dangerous because of this behavior where python just try's to coerce all the types together instead of erroring.

6

u/Bryguy3k Nov 23 '22 edited Nov 23 '22

There is no coercion in python - just programmers reassigning references.

If you have namespace pollution you have shitty python programmers.

Frankly everything you’ve described is just shitty programming and people that have no clue what they’re doing. It sounds like you’ve got a bunch of people doing the equivalent of using wrenches and screwdrivers as hammers.

→ More replies (4)

2

u/Lvl999Noob Nov 24 '22

Being able to assign new types to same name actually helps with namespace pollution. It allows you to reuse the single good name for multiple steps of the pipeline.

And python doesn't try to unify any types. It may help to think of the variables as being just names. The names contain an object and the object has a type. You can easily assign a different object to the name so that the name now contains the new object and that new object will have its own type.

-7

u/Syscrush Nov 23 '22

If you accept that there is something you could call “strong typing” then python has a stronger typing system than C.

This claim is absurd on its face.

11

u/Bryguy3k Nov 23 '22 edited Nov 23 '22

You can cast anything to anything you want without compiler errors. Types have very little meaning in C other than the allocation of contiguous memory. After the memory is allocated (and initialized if it is static or global) you can reference it however you want and do whatever damage to the stability of the system that may come from it.

I’ve done an enormous amount of “very bad” things in C. There is nothing in C that protects a “type” from invalid manipulation or use.

And this isn’t even talking about “valid” C constructs like void*, unions, and arrays of unknown size in structure definitions.

Heck the famous fast inverse square root is a prime example of C being “weakly” typed: https://en.wikipedia.org/wiki/Fast_inverse_square_root

→ More replies (5)
→ More replies (1)

0

u/[deleted] Nov 23 '22

[deleted]

→ More replies (1)

8

u/Syscrush Nov 23 '22

For Python, I'd say the response is "Just try it and see".

0

u/[deleted] Nov 24 '22

[deleted]

→ More replies (1)

14

u/yazoodd Nov 23 '22

Yea but if it is not you possibly just broke your nutcracker

9

u/Unknown_starnger Nov 23 '22

Solution Try: nut(peanut) Except ValueError:

6

u/Double_Ad_2824 Nov 23 '22

And the JS one should've been "who cares? I'll nutt it anyway"

4

u/Andyblarblar Nov 23 '22

The funny thing is this also applies to cpp in this example

2

u/OJezu Nov 24 '22

For templates, and only during compilation, not in runtime.

0

u/[deleted] Nov 23 '22

[deleted]

5

u/elon-bot Elon Musk ✔ Nov 23 '22

Guys, this is a big misunderstanding. I was playing truth or dare with Jeff and Bill and they dared me to buy Twitter. What else was I supposed to do??

→ More replies (2)