Python is a duck-typed language.
Duck typing is a style of dynamic typing in which an object's methods and properties determine the valid semantics, rather than the inheritance from a specific
class or implementation of a particular interface.
This feature can be demystified by python's functions. They take parameters of any time, and if they can't proceed with an object at a step, it gives assersion.
function calculate(a, b, c) => return (a+b)*c
example1 = calculate (1, 2, 3)
example2 = calculate ([1, 2, 3], [4, 5, 6], 2)
example3 = calculate ('apples ', 'and oranges, ', 3)
print to_string example1
print to_string example2
print to_string example3
And in non-duck-typed language like c++, you have to overload the function for different types of object, or use template.
764




被折叠的 条评论
为什么被折叠?



