Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand that everything is an object in Python

Shulou Source: shulou.com Published: 2022-06-02 18:33:11 09月22日 Update

This article introduces the relevant knowledge of "how to understand everything in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Data types as objects

All data types, such as numbers, strings, lists, and dictionaries, are objects in Python. Think of it as a data box. Some boxes become smaller simply because they hold int, bool, or str, while others become larger because they hold list or dict. The difference is that their size reflects the memory needed to store this data in the computer.

Of course, a box can also have other features, such as color and material, which can be defined as different types of data related to the box.

As objects, you can apply the following common meanings or functions:

Can be used as the return value of the function.

Can be passed to the function as an argument.

You can assign a value to a variable.

Can be associated with other objects as part of the data.

# Assign an int to a variable a = 5 # Use an int asan argument in a function defadd_two (a): return a + 2 add_two (6) # Return astring in a function defis_even (a): Return'even'if a% 2% True 0else expensive ODD 'is_even (745) # A booleanvalue in a tuple 7, 8)

What it means as a Python object

You can see examples of these meanings of Python objects, such as int and string, in the above code snippet. It's not hard to understand even if you don't know much about Python coding or any programming language.

So why do people say that functions are also objects in Python?

Function as an object

Before we get down to business, let's review what a function is: a function is a named block of code, accepts zero or more inputs, and returns output after running some calculations.

The following is a simple example that illustrates the key components highlighted in the above definition:

# Assign an int to a variable a = 5 # Use an int asan argument in a function defadd_two (a): return a + 2 add_two (6) # Return astring in a function defis_even (a): Return'even'if a% 2% True 0else expensive ODD 'is_even (745) # A booleanvalue in a tuple 7, 8)

In the previous section, we mentioned some common features of Python data objects. The following example will show you how a function provides this kind of functionality.

Use a function as an argument to another function

Because a function is an object, you can pass the function as an argument to another function. As shown below, three functions are created: combine_two_numbers (), add_two_numbers (), and multiply_two_numbers (), which is used to calculate the sum and product of two numbers in the tuple.

Unlike commonly seen functions, the functions add_two_numbers and multiply_two_numbers are passed as arguments, which further calculates the sum and product of these numerical tuples, respectively.

Function as an argument to a function

Variable assignment

In the following code snippet, I create a function called greeting () and assign it to the variable say_hello, which produces the same output as greeting ().

Assign a function to a variable

One thing to note is that both the greeting () and say_hello () functions point to the same object in memory. For example, if you check their addresses, they all have the same values as shown below. If you delete the greeting () function, the name "greeting" becomes undefined, and the deletion does not affect potential objects referenced by the "sayhello" function.

Therefore, there is an important concept here that the variables that reference the function are different from the actual objects stored in memory.

Operation function

Function as a return value

As an object, the Python function can be used as a valid return value of the function. Looking at the following example, the author wrote a function called add_number_creator (), which returns a function that can add numbers to integer parameters.

The functions add_three () and add_five () are created by calling the add_number_creator () function. These two functions complete the expected task by adding 3 and 5 to the integers specified in the function call.

Function as a return value

Function as part of another object

We can also create a list of functions to make them part of the list object. Following the previous example, we used the add_number_creator () function to create three functions, entering integers to add 0, 1, and 2, respectively. As you can see, you can enumerate a list of functions, just as you usually do with a list of integers or strings.

This is the end of "how to understand everything in Python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Functions objects data parameters variables differences numbers integers boxes examples code memory function meaning type relevance output same three two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Huawei OPPO Reno Xiaomi