Skip to main content
https://english.stackexchange.com/a/4646/109567
Source Link
Wai Ha Lee
  • 8.8k
  • 7
  • 45
  • 70
added 3 characters in body
Source Link
ticster
  • 786
  • 3
  • 4
def square(n: int):
    return int(n)**int(n)
def square(n: int):
    return int(n)*(n)
def square(n: int):
    return int(n)*int(n)
added 7 characters in body
Source Link
ticster
  • 786
  • 3
  • 4

First, the moreone obvious approach :

def square(n: int):
    return n*nint(n)*(n)

Now, what distinguishes these approaches ? Strictly speaking, all 3 of them fit the bill and answer the question. What is informative to a developer is understanding the ups and downs of each approach. For example, the first option will also work with some strings whereas the second will fail. This can be good or bad depending on context, and can lead to all sorts of discussions about best practices around dynamic typing in Python, like for example should some functions strictly check type or should you embrace duck typing at all times ?

First, the more obvious approach :

def square(n: int):
    return n*n

Now, what distinguishes these approaches ? Strictly speaking, all 3 of them fit the bill and answer the question. What is informative to a developer is understanding the ups and downs of each approach. For example, the first option will also work with strings whereas the second will fail. This can be good or bad depending on context, and can lead to all sorts of discussions about best practices around dynamic typing in Python, like for example should some functions strictly check type or should you embrace duck typing at all times ?

First, one obvious approach :

def square(n: int):
    return int(n)*(n)

Now, what distinguishes these approaches ? Strictly speaking, all 3 of them fit the bill and answer the question. What is informative to a developer is understanding the ups and downs of each approach. For example, the first option will also work with some strings whereas the second will fail. This can be good or bad depending on context, and can lead to all sorts of discussions about best practices around dynamic typing in Python, like for example should some functions strictly check type or should you embrace duck typing at all times ?

Source Link
ticster
  • 786
  • 3
  • 4
Loading