Yahoo India Web Search

Search results

  1. Jun 17, 2011 · An @ symbol at the beginning of a line is used for class and function decorators: PEP 318: Decorators. Python Decorators - Python Wiki. The most common Python decorators are: @property. @classmethod. @staticmethod. An @ in the middle of a line is probably matrix multiplication: @ as a binary operator.

  2. Aug 31, 2008 · A Python dict, semantically used for keyword argument passing, is arbitrarily ordered. However, in Python 3.6+, keyword arguments are guaranteed to remember insertion order. "The order of elements in **kwargs now corresponds to the order in which keyword arguments were passed to the function." - What’s New In Python 3.6. In fact, all dicts in ...

  3. Jan 17, 2013 · Simply -> is introduced to get developers to optionally specify the return type of the function. See Python Enhancement Proposal 3107. This is an indication of how things may develop in future as Python is adopted extensively - an indication towards strong typing - this is my personal observation.

  4. Jan 6, 2022 · The easiest way is to use math.factorial (available in Python 2.6 and above): import math. math.factorial(1000) If you want/have to write it yourself, you can use an iterative approach: def factorial(n): fact = 1. for num in range(2, n + 1): fact *= num. return fact.

  5. Dec 6, 2020 · If you want such a nested function to be available at the module level, you'd have to either return it from the function, or define a global variable to store it in: def f1(a): def f2(x): return a+x. return 2*a, f2. then call that as result, f2 = f1(somevariable_or_literal).

  6. 11. Yes, it's possible. I wrote the code below in Python 3.2.1: def overload(*functions): return lambda *args, **kwargs: functions[len(args)](*args, **kwargs) Usage: myfunction=overload(no_arg_func, one_arg_func, two_arg_func) Note that the lambda returned by the overload functions choose a function to call depending on the number of unnamed ...

  7. Nov 26, 2015 · 9. Python has nothing called pointers, but your code works as written. Function are first-class objects, assigned to names, and used as any other value. You can use this to implement a Strategy pattern, for example: def the_simple_way(a, b): # blah blah. def the_complicated_way(a, b): # blah blah. def foo(way):

  8. In Python, the use of an underscore in a function name indicates that the function is intended for internal use and should not be called directly by users. It is a convention used to indicate that the function is "private" and not part of the public API of the module.

  9. If you really need to get the result from your function by assigning to a global variable, use the global keyword to tell Python that the name should be looked up in the global scope: words = ['hello'] def replace_global_words(): global words words = ['hello', 'world'] replace_global_words() # `words` is a new list with both words

  10. Mar 2, 2010 · Read about using docstrings in your Python code. As per the Python docstring conventions: The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated.