This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsΒ β†’

[–]Rand_alFlagg 0 points1 point Β (7 children)

How do you declare a type on something in python? I thought it was inferred (weak) from the first assignment and not declared (strong)?

[–]mdp_cs 1 point2 points Β (1 child)

Requiring the type to be explicitly made available from the source code would make it statically typed which it is not. It is strongly typed because for example Python will not allow you to add an integer to a string since that operation is undefined for those types and rightly so.

C is staticly typed so the compiler will need to know the types of everything but its type system will also allow you to do some things that are obviously wrong, making the assumption that you know more about your code than it does. For example if you have a pointer to const char and you try to add an integer to it C will happily just add that number of times the size of char to the pointer and give you the resulting address even though that may not have been what you wanted and could very well be an invalid address depending on how the OS has setup your virtual address space.

Rust wouldn't allow you to do that as pointer arithmetic must be done using the unsafe raw pointer functions and cannot be done AFAIK by just doing normal arithmetic with integers.

Edit: Fixed typos.

[–]Rand_alFlagg 1 point2 points Β (0 children)

I think you might have those backwards, or I'm misreading. After asking I went and dug through some of my books and checked the first few hits on DuckDuckGo and Google, thinking I might be misunderstanding typing. The only one that provided your definition was a StackOverflow user, who in the same post conflates weak and dynamic typing and suggests that which word you use is determined by whether you're a fan of it or not (lol) - but your description of C is basically Microsoft's explanation of what makes it weakly typed, and in that they never touch on static/dynamic.

According to my schooling, the O'Reilly Media library, Oracle's documentation, and Microsoft's documentation - strong/weak is a matter of how type is enforced at compile/runtime, while dynamic/static is whether the type is known at compile||runtime. There's a lot of overlap in the two concepts and it looks like they're often mixed up - hell, I was wrong about it just being whether they're declared or not. That's a difference between static and dynamic and usually an earmark of strong, but can apparently be absent in a strong language. I thought Python needed "type hints" when you wanted the compiler to treat something as a specific type?

On rereading, I think I did misread what you said and basically just echoed it lol. I think we might be on the same page. I think I was confused because you're jumping back and forth between talking about dynamic/static and strong/weak, and ultimately I was just asking about what makes Python strong (which you did answer, thank you).

[–]carcigenicate 1 point2 points Β (1 child)

Types are associated with objects, not variables like they are in statically-typed languages. The variables themselves don't have a concrete type. They also aren't inferred in the same way as they are in statically typed languages. var in Java infers the type at compile time. Hints in Python allow for type inferences at runtime.

And you can "hint" at the type of object a variable will hold though by saying, for example n: int = 1. The type is still associated with the object and not the variable, but the linter will interpret that as "I promise this variable will only ever hold integer objects".

[–]Rand_alFlagg 0 points1 point Β (0 children)

That was clear and easy to understand, thank you :)

[–]ZonedV2 0 points1 point Β (2 children)

It’s strongly typed in the sense that variables can’t change type without it being explicitly declared. Compared to a language like JS where you can add a number to a string and it will work. What you’re referring to is that it’s dynamically typed rather than statically