this post was submitted on 21 Nov 2023
250 points (89.8% liked)

Programmer Humor

32464 readers
163 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] CanadaPlus@futurology.today 41 points 11 months ago* (last edited 11 months ago) (4 children)

Yes. Please. Although something strongly typed would be even better. It's ridiculous the world runs on a language built in 2 weeks.

[–] Gebruikersnaam@lemmy.ml 27 points 11 months ago (1 children)

Python is strongly typed, but it is also dynamically typed.

[–] CanadaPlus@futurology.today 9 points 11 months ago (2 children)

TIL. Obviously I've avoided using it much.

So how does that work? Is there a few implicit conversions that are allowed, but if you really write something weird it will complain?

[–] Gebruikersnaam@lemmy.ml 11 points 11 months ago (1 children)

Yes, it has no implicit conversions like JS or R. It does, however, allow you to not specify the type of a variable and even change it without complaining. Even if you add types these are only hints that won't generate errors unless you use external type checking (e.g. mypy).

[–] tryptaminev@feddit.de 9 points 11 months ago (2 children)

example:

i = 5.0//2

list[i]

throws an error because i is double and the list-index expects an integer.

so for it to work the code needs to look like this:

i = int(5.0//2)

list[i]

meanwhile this works:

i=5

i= 'abcde'

[–] Chrobin@discuss.tchncs.de 2 points 11 months ago (1 children)
[–] Gebruikersnaam@lemmy.ml 3 points 11 months ago (1 children)

It is but if you start with a float you get a float back.

[–] Chrobin@discuss.tchncs.de 2 points 11 months ago (1 children)

You're right, I did not know that. Thanks!

[–] Gebruikersnaam@lemmy.ml 1 points 11 months ago

Was really surprised by this too, because iirc Python 2 did not do this.

[–] dzervas@lemmy.world 1 points 11 months ago (1 children)

you can do i: int to make this error out

[–] Gebruikersnaam@lemmy.ml 6 points 11 months ago (1 children)

No, type hints are not enforced.

[–] HappyRedditRefugee@lemm.ee 6 points 11 months ago

In python you always have the right type, cause everything is an object

[–] words_number@programming.dev 24 points 11 months ago (1 children)

Python is actually mostly strongly typed. Strongly (e.g. can't use a number as a string without explicitly converting it), but dynamically (can change type of variable at runtime). You probably would prefer a statically typed language and I agree.

[–] CanadaPlus@futurology.today 1 points 11 months ago* (last edited 11 months ago) (1 children)

Alright, thanks for the help with terminology. I'm a bit confused about changing types at runtime. I thought a compiled or interpreted language stopped having types at runtime, because at that point it's all in assembly. (In this case of course it's scripting, which someone pointed out to me elsewhere)

[–] CapeWearingAeroplane@sopuli.xyz 3 points 11 months ago (2 children)

That's a compiled language, an interpreted language is translated to assembly at runtime, in pythons case: pretty much one line at a time.

Disclaimer: To the best of my knowledge, please correct me where I'm wrong.

[–] Skyhighatrist@lemmy.ca 2 points 11 months ago* (last edited 11 months ago)

That's really only native compiled languages. Many popular languages, such as C#, Java, etc. Lie somewhere in between. They get compiled to intermediary byte code and only go native as the very final step when running. They run in a runtime environment that handles that final step to execute the code natively. For .NET languages that's the CLR (Common Language Runtime).

For .Net the process goes like this:

  • You write the code
  • Code is compiled to MSIL
  • At runtime when the MSIL is executing a JIT (just-in-time) compiler translates the MSIL into native code.
  • The native code is executed.

Java has a similar process that runs on the JVM. This includes many, many languages that run on the JVM.

~~JavaScript in the browser goes through a similar process these days without the intermediary byte code.~~ Correction, JS in modern browsers also follow this process almost exactly. a JIT compiler compiles to bytecode which is then executed by the browser's JS engine. Historically JS has been entirely interpreted but that's no longer the case. Pure interpreted languages are pretty few and far between. Most we think of as interpreted are actually compiled, but transparently as far as the dev is concerned.

Last, but certainly not least, Python is also a compiled language, it's just usually transparent to the developer. When you execute a python program, the python compiler also produces an intermediary bytecode that is then executed by the python runtime.

All that being said, I welcome any corrections or clarifications to what I've written.

[–] CanadaPlus@futurology.today 0 points 11 months ago* (last edited 11 months ago) (1 children)

I did know the difference, but I didn't realise it ran one line at a time! I had kind of assumed it at least did one pass through everything before giving output. Thanks.

[–] CapeWearingAeroplane@sopuli.xyz 1 points 11 months ago

I believe it does "one pass" when it loads the code into ram, because syntax errors can be caught before anything runs. But I think the actual interpretation happens pretty much one line at a time :)

[–] PeWu@lemmy.ml 5 points 11 months ago (1 children)
[–] Zangoose@lemmy.world 11 points 11 months ago (2 children)
[–] skullgiver@popplesburger.hilciferous.nl 4 points 11 months ago* (last edited 11 months ago)

[This comment has been deleted by an automated system]

[–] PeWu@lemmy.ml 2 points 11 months ago

Couldn't agree more.

[–] lukas@lemmy.haigner.me 4 points 11 months ago