Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand that everything is an object in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to understand everything in python? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

It is well known that python is an object-oriented language, in python language, it can be said that everything in python is object-oriented. If you have studied java, you will also know that java is also an object-oriented language, but by contrast, python is the real object-oriented language.

1. What is object-oriented?

The object can be said to be a concrete thing, but the object-oriented is not face-to-face with the object, but manipulated by the method of things. Let me give you an example. I go to a restaurant and ask the waiter to order. Here, uh, it can be said that I called the waiter's method of ordering. The waiter here can be said to be an object, and I am the caller. After ordering, the waiter will go to the store and ask the cook to stir-fry. Here, the chef is also an object, and cooking is a method for chefs.

Objects in 2.python

It is true that everything in python is an object, because the classes and functions of python are treated as objects, which belong to the first-class citizens of python. First-class citizens need to meet the following four conditions:

Can assign a value directly to a variable

Can be added to the collection object

Can be passed as a function parameter

Can be returned as a function

When you see here, you can see that everything in python is indeed an object, because it all belongs to first-class citizens.

All objects in python have three characteristics.

Identity, that is, the storage address, can be queried by id ()

Type, that is, the type to which the object belongs, can be queried with the type () method

Value, each has its own data

Query the identity of the object

N1 = 1

N2 = 2

Print (id (N1))

Print (id (N2))

# here are the results

1956798624

1956798656

Query the type of object

Print (type (N1))

Print (type (N2))

# here are the results

You can see that both types are the same. Of course, here we can use the isinstance () method to see if the object belongs to a certain type, but we won't expand it here.

3. The relationship between type journal object and class

Object is inherited by all classes and is a base class of python, but type is also a type, and the type of object is type, and type is also an instance of its own. At this point, you may not believe it, but we will have verification below. Type, a class that even refuses to let go of itself, has to implement its own object, and other classes can be imagined, so it is more fully explained here that everything in python is an object.

Verify it.

Print (type (object))

Print (type (type))

# here are the results

As you can see, type is an example of itself, and I'll talk more about objects later.

There is also class in python, that is, classes are generated through type, while objects are generated through classes, there is the following relationship: type-- > class-- > object. Let's take a look at the code.

Class A ():

Pass

A = A ()

Print (type (a))

Print (type (A))

# here are the results

Here you can see that type has two functions, one is to return the type of an object, and the other is to generate a class.

This is the answer to the question on how to understand everything in python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report