Relative Content

Tag Archive for duck-typing

Python Forgiveness vs. Permission and Duck Typing

In Python, I often hear that it is better to “beg forgiveness” (exception catching) instead of “ask permission” (type/condition checking). In regards to enforcing duck typing in Python, is this

How can a statically typed language support duck typing?

I understand what dynamic and static type systems are, and what duck typing is. But I don’t understand how you can have a static language that supports duck typing. To my understanding only a dynamically typed language can support duck typing.

Is there a way to prevent variables from changing their type in Python?

It is useful to have the interpreter derive the type of a variable automatically. This on its own is similar to the auto keyword in C++11. However, in Python variables can change their type after being declared for the first time. This potentially can cause a lot of errors. I haven’t seen any reasonable use case for this, besides changing from or to None.

Is there a difference between duck typing and structural typing? [duplicate]

This question already has answers here: Type systems: nominal vs. structural, explicit vs. implicit (4 answers) Closed 9 years ago. Duck typing is deciding on the type of an object, based on the operations it supports and the attributes it owns. Structural typing is… the same thing? What exactly is the difference? type-systems duck-typing 1 […]

Is there a difference between duck typing and structural typing? [duplicate]

This question already has answers here: Type systems: nominal vs. structural, explicit vs. implicit (4 answers) Closed 9 years ago. Duck typing is deciding on the type of an object, based on the operations it supports and the attributes it owns. Structural typing is… the same thing? What exactly is the difference? type-systems duck-typing 1 […]