About 11,100,000 results
Open links in new tab
  1. What does -> mean in Python function definitions? - Stack Overflow

    Jan 17, 2013 · funcdef: 'def' NAME parameters ['->' test] ':' suite The optional 'arrow' block was absent in Python 2 and I couldn't find any information regarding its meaning in Python 3. It turns out this is correct Python and it's accepted by the interpreter: def f(x) -> 123: return x I thought that this might be some kind of a precondition syntax, but:

  2. How can I return two values from a function in Python?

    def newFn(): #your function result = [] #defining blank list which is to be return r1 = 'return1' #first value r2 = 'return2' #second value result.append(r1) #adding first value in list result.append(r2) #adding second value in list return result #returning your list ret_val1 = newFn()[1] #you can get any desired result from it print ret_val1 # ...

  3. "TypeError: 'type' object is not subscriptable" in a function signature

    Aug 18, 2020 · # At the top of the code from typing import List # when annotating the function def twoSum(self, nums: List[int], target: int) -> List[int]: Note the capital L in List. Python 3.5 and 3.6. The __future__ annotation is not supported. Use the typing module. Python 3.4 and below. Type annotations are not supported at all. Simply remove them:

  4. What do * (single star) and / (slash) do as independent parameters?

    Jan 9, 2020 · def pow(x, y, z=None, /): "Emulate the built in pow() function" r = x ** y return r if z is None else r%z Another use case is to preclude keyword arguments when the parameter name is not helpful. For example, the builtin len() function has the signature len(obj, /). This precludes awkward calls such as:

  5. How can I use `def` in Jenkins Pipeline? - Stack Overflow

    Nov 10, 2017 · As @Rob said, There are 2 types of pipelines: scripted and declarative.It is like imperative vs declarative.def is only allowed in scripted pipeline or wrapped in script {}.

  6. How can I use a global variable in a function? - Stack Overflow

    Jul 11, 2016 · You may want to explore the notion of namespaces.In Python, the module is the natural place for global data:. Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module.

  7. What does if __name__ == "__main__": do? - Stack Overflow

    Jan 7, 2009 · It executes the def block, creating a function object, then assigning that function object to a variable called function_a. It prints the string "before function_b". It executes the second def block, creating another function object, then assigning it to a variable called function_b. It prints the string "before __name__ guard".

  8. python - Why use def main()? - Stack Overflow

    Oct 28, 2010 · Variables inside def main are local, while those outside it are global. This may introduce a few bugs and unexpected behaviors. But, you are not required to write a main() function and call it inside an if statement. I myself usually start writing small throwaway scripts without any kind of function.

  9. correct way to define class variables in Python - Stack Overflow

    Neither way is necessarily correct or incorrect, they are just two different kinds of class elements: Elements outside the __init__ method are static elements; they belong to the class.

  10. How do I define a function with optional arguments?

    def add(x,y): return x+ y # calling this will require only x and y add(2,3) # 5 If we want to add as many arguments as we may want, we shall just use *args which shall be a list of more arguments than the number of formal arguments that you previously defined ( x and y ).

Refresh