Home > Backend Development > Python Tutorial > The difference between Python class objects and instance objects

The difference between Python class objects and instance objects

(*-*)浩
Release: 2019-07-05 10:21:46
Original
7125 people have browsed it

Class objects and instance objects

The difference between Python class objects and instance objects

Short theory: (Recommended learning: Python video tutorial)

A class object summarizes and abstracts objects with similar attributes and methods into a class object. It can define some similar attributes and methods. Different instance objects refer to the attributes and methods of the class object, which can reduce the duplication rate of code. .

Instance objects are also called instantiated objects. They are not abstract but a specific object in a class of objects.

Metaphor understanding:

I believe some people find it a bit confusing, so I will explain it with a metaphor here, I hope you can understand. First of all, you must understand that in python, "everything is an object". Personal understanding:

Class objects are like a basket of "fruits". If the fruits in the basket are not repeated, then the "fruits" in the basket belong to the same class and are abstract, just like someone said to you "Give me fruit", your first reaction must be "What fruit?", so it is uncertain.

The instance object, like the "apple" in the basket, is a specific object, that is, an instance. I think someone said to you "Give me an apple". You probably don't want to choose which fruit.

class Main_class_dll():
      

      def __init__(self):
        dllName = "SessionConnector.dll" 
        dllABSPath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + dllName
        self.dll = cdll.LoadLibrary(dllABSPath)
        self.session_id=''

      def int_create_(self):
         self.dll.Init.restype = c_bool
         sign = self.dll.Init()

      def Create_Session(self,ip,port):
        self.dll.CreateSession.argtypes=[c_char_p,c_char_p]   #输入参数的格式
        self.dll.CreateSession.restype = c_int;               #输出参数的格式
        self.session_id = self.dll.CreateSession(ip,port);

      def send_recv(self,buf):
        time.sleep(2)
        self.dll.SendSessionMsg.restype = c_bool;
        self.dll.SendSessionMsg.argtypes=[c_int,c_char_p,c_uint]
        ret = self.dll.SendSessionMsg(self.session_id, buf, len(buf) + 1);
        self.dll.RecvSessionMsg.argtypes=[c_int,c_char_p,c_uint,c_int]
        self.dll.RecvSessionMsg.restype = c_bool;
        recv_buf = create_string_buffer(1024);
        ret = self.dll.RecvSessionMsg(self.session_id, recv_buf, 1024, 3000);

        self.dll.DestroySession.restype = c_bool;
        ret = self.dll.DestroySession(self.session_id);

        return recv_buf.value
Copy after login

A class object is an object that can be directly represented by the class name. It supports two operations, direct attribute use and instantiation. For the use of class attributes, just use class name.attribute directly. For the use of class methods, you need to instantiate an object and assign the object name to self for use, as shown below:

class test:
    data = 1
    def __init__(self):
        self.property=0

    def test2(self):
        print 'hello'

if __name__=='__main__':
    t = test()
    print test.data
    print t.data
    print test.test2
    print t.test2()
    print test.test2(t)
Copy after login

For more Python-related technical articles, please visit the Python Tutorial column Get studying!

The above is the detailed content of The difference between Python class objects and instance objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template