Python functions

Updated on technology 2024-05-06
8 answers
  1. Anonymous users2024-02-09

    #python

    This can be done with a decorator.

    However, there is generally no restriction on parameter types.

    Here's an idea:

    def argfilter(*types):

    def deco(func): This is the decorator.

    def newfunc(*args): New function.

    if len(types)==len(args):

    correct = true

    for i in range(len(args)):

    if not isinstance(args[i], types[i]): determines the type.

    correct = false

    if correct:

    return func(*args) returns the value of the original function.

    else:raise typeerror

    else:raise typeerror

    return newfunc is a new function returned by the decorator.

    return deco returns a function as a decorator.

    argfilter(int, str) specifies the parameter type.

    def func(i, s): Defines the function to be modified.

    print i, s

    After that, if you want to limit the type, that's it:

    @argfilter (the class name of the first parameter, the class name of the second parameter, ..)The class name of the nth parameter).

    def yourfunc(first argument, first argument, ..)nth parameter):

    Equivalent to: def yourfunc(first argument, first argument, ..)nth parameter):

    yourfunc = argfilter(The class name of the first parameter, the class name of the second parameter, ..)class name of the nth parameter) (yourfunc).

  2. Anonymous users2024-02-08

    Class class: A collection of objects that describe the same properties and methods. Defines the properties and methods that are common to each object in the collection. An object is an example of a class.

    When the class definition is complete (exiting gracefully), a class object is created. Basically, it's a wrapper around the namespace created by the class definition; We'll take a closer look at class objects in the next section. The original local scope (the one that was in effect before the class definition was introduced) is restored, and the class object is bound to the class name (in this case, classname) at the head of the class definition.

    Basic grammarOne of the goals of Python's design is to make it highly readable. It is designed to use punctuation marks and English words that are often used in other languages as much as possible, so that it looks neat and beautiful. It doesn't require repetition of declarative statements like other static languages like C and Pascal, nor does it have special cases and surprises like their syntax.

    The above content reference: Encyclopedia-python

  3. Anonymous users2024-02-07

    1. Function definition

    Use the def keyword to define the function.

    def function name (parameter 1.)Parameter 2Parameter 3....

    "The docstring, docstring, is used to describe what a function does"""

    Function body. return expression.

    What annotations do: Explain what the function does and what the function does.

    When encountering a colon, it is necessary to indent it, and all the indented blocks after the colon constitute the function body, which describes what the function does, that is, what is the function of the function. The essence of Python functions is consistent with the essence of functions in mathematics.

    2. Function call

    Functions must be defined before they can be called, otherwise an error will be reported.

    The call of the function without a parameter: function name (), and the call of the function when there is a parameter: the function name (parameter 1Parameter 2

    Don't call itself in the body of the function when defining the function, otherwise you won't be able to get out and get stuck in a loop call.

    Functions need to be called in order to be executed, and simply defining a function will not be executed.

    In the debug tool, step into the called function, and step into my code into the called module.

  4. Anonymous users2024-02-06

    Function Definition and Call:

    1) >def function name (parameter 1, parameter 2): Definition.

    .Function body. Function name (argument 1, argument 2).

  5. Anonymous users2024-02-05

    The class key word is used by Ranzhou to describe class objects. Oak orange.

  6. Anonymous users2024-02-04

    A function is a well-organized, reusable segment that implements a related function.

    Functions improve reuse and application modularity.

    In addition to the functions that come with Python, you can also create your own functions, which are called custom functions.

    Syntax: Function blocks start with def.

    The parameters areEnter a value, put it in the slogan after the function name, and the function content starts with a colon: , the function body is indented, and return is returnedOutput value

    Function calls use keyword arguments to determine the value of the argument passed in, and if there are multiple functions, they don't need to be in the specified order.

    When you define the number of office sources, specify the default value of the parameter. If no arguments are passed in when calling, the default value is used.

    If no parameter is specified for an indefinitely long part, the indefinite part is an empty tuple.

    AddedTwo asterisks** is imported as a dictionary.

    It is used to indicate that the function parameters must use the specified positional parameters, not the form of keyword parameters.

    It can only be used after the version.

    Lu Song uses def to define functions, and there is no function name.

    The body of lamdba is an expression, not a ** block, and the function body is much simpler than def.

    What is defined inside a function is a local variable and can only be used inside a function.

    Defined outside of the function are global variables that can be used globally.

    A module is a file that contains all defined functions and variables, and generally groups of functions of the same kind are called modules.

    After the module is imported, the corresponding function is called to use it.

    How to import modules:

    Import a specified section from the module.

    Import all the content of a module.

  7. Anonymous users2024-02-03

    There can be class methods (look up what is a class method) and can be used without instantiation.

    The main difference between classes and functions in Python is that classes can have variables and various methods, while functions do not. Functions can only be run, with or without returning values.

    If a module doesn't declare the class private, other modules can use the class by importing the module and then using it"Module name. Class name"to call.

  8. Anonymous users2024-02-02

    1. Class Definition:

    class "Class Name":

    Once the statement class is instantiated, its properties can be used, in fact, after a class is created, its properties can be accessed by the class name.

    If you modify the properties of a class directly with its name, it will directly affect the objects that have already been instantiated.

    Private properties of the class:

    private attrs states that the property is private and cannot be used or accessed directly outside of the class.

    When used in a method inside a class self__private_attrs

    class method. Inside the class, you can use the def keyword to define a method for a class, and unlike a general function definition, a class method must contain the parameter self, which must be the first argument.

    Private class methods.

    private method starts with two underscores, stating that the method is private and cannot be called outside the class.

    Call slef. inside the class__private_methods

    Proprietary methods of the class:

    The init constructor, which is called when an object is generated.

    del destructor, which is used when disposing of an object.

    REPR Print, Convert.

    setitem is assigned by index.

    getitem gets the value by index.

    len to get the length.

    CMP Comparative Operations.

    call function call.

    add.

    subtract operations.

    mul multiplication.

    div division.

    mod remainder.

    pow weighed square.

    Example: [python] view plain copy

    Class definitions. class people:

Related questions
12 answers2024-05-06

Functions in a class can be defined in no particular order. This is a feature of functions, so he doesn't use any order to compose them.

20 answers2024-05-06

Knowing the analytic formula to define the domain: just make sure that the formula is meaningful, for example, the denominator is not 0, the base is not 0 under the even root number, the base of the 0 power is not 0, the true number of the logarithmic formula is greater than 0, the base is greater than 0 and not 1, etc. >>>More

16 answers2024-05-06

The analytic formula of the function is f(x)=3x-1, using the matching method: f(x+1)=3x+2=3(x+1)-1, that is, f(x)=3x-1, it can also be changed, so that x+1=t, then x=t-1, f(t)=3(t-1)+2=3t-1, that is, f(x)=3x-1, the list method means that the ellipsis is missing. Three points, both front and back. >>>More

9 answers2024-05-06

The function definition consists of three parts: the return type, the function identifier, the required parameters of the function, and the function body, for example. >>>More

15 answers2024-05-06

This kind of problem can be exhausting.