Python binding methods and non-binding methods

Source: Internet
Author: User
Tags md5 hash uuid

Used in:

The import UUID--------------UUID is a globally unique identifier of 128 bits, usually in the form of a 32-bit string.

UUID.UUID1 ()-------------generate unique UUID based on MAC address, timestamp, random number to ensure uniqueness worldwide

UUID.UUID3 ()-------------gives a UUID by calculating the MD5 hash value of a namespace and name, so that different names in the namespace have different uuid, but the same name is the same UUID.

Uuid.uuid4 ()-------------The UUID is obtained by pseudo-random number, there is a certain probability repetition

UUID.UUID5 ()-------------and UUID3 are basically the same, except that the hashing algorithm used is SHA1

@classmethod---------is added to the binding method defined to the class (there should be a class that is bound to the class to invoke, but the object can actually be used, except that the automatic pass-through is still a class)

@staticmethod---------Add to the defined non-binding method function (but no matter who calls, there is no automatic value of the effect, is a normal function)

1. Binding method

Attributes: The binding to who should call, who will call the first parameter will be automatically passed in << is the essence of automatic value >>

The binding methods fall into two categories:

1. Bind to an object method:

functions defined inside a class (not decorated by any adorner), are bound to objects by default

2. Methods to bind to a class:

A function defined inside a class that is decorated with the adorner @classmethod is bound to the class and should be called by the class, which automatically passes the class as the first parameter when the class is called.

2. Non-binding methods

If a function defined in a class is decorated by an adorner @staticmethod, the function becomes a non-binding method

Neither binding to the class nor binding to the object means that both the class and the object can be called

But no matter who calls, there is no automatic value-transfer effect, is a common function

3. Application

If the function body code requires an externally passed-in class, the function should be defined as a method bound to the class

If the function body code requires an externally passed-in object, the function should be defined as a method bound to the object

If the function body code does not require an externally passed-in class and does not require an externally-passed object, the function should be defined as a non-binding method/Normal function

For example:

1 classFoo:2 @classmethod3     defF1 (CLS):4         Print(CLS)5 6     defF2 (self):7         Print(self)8 9obj =Foo ()Ten  One Print(OBJ.F2)
>>><bound method Foo.f2 of <__main__. Foo object at 0x000001f2d4afccc0>> A Print(FOO.F1)
>>><bound method Foo.f1 of <class ' __main__. Foo ' >> - Foo.f1 ()
>>><class ' __main__. Foo ' > - Print(Foo)
>>><class ' __main__. Foo ' >

The F1 is bound to the class

There should be classes that are bound to the class to invoke, but the object can actually be used, except that the automatic pass-through is still the class

1 Print (FOO.F1)
>>><bound method Foo.f1 of <class ' __main__. Foo ' >>2print(OBJ.F1)
>>><bound method Foo.f1 of <class ' __main__. Foo ' >>3foo.f1 ()
>>><class ' __main__. Foo ' >4 obj.f1 ()
>>><class ' __main__. Foo ' >

F2 is a binding to an object.

1 Obj.f2 ()
>>><__main__. Foo object at 0x00000203677cccf8>2 foo.f2 (obj)
>>><__main__. Foo Object at 0x00000203677cccf8>

Example (print IP and port):

1 ImportSettings2 ImportUUID3 4 classMysql:5     def __init__(self, IP, port, net):6Self.uid =Self.create_uid ()7Self.ip =IP8Self.port =Port9Self.net =NetTen  One     defTell_info (self): A         Print('%s:%s'%(Self.ip, self.port)) -  - @classmethod the     deffrom_conf (CLS): -         returnCLS (Settings. IP, settings.net, settings. PORT) -  - @staticmethod +     deffunc (x, y): -         Print('do not bind to anyone') +  A @staticmethod at     defcreate_uid (): -         returnUUID.UUID1 ()

Default instantiation Mode: Class name (...)

obj = Mysql ('10.10.0.9', 3307)

A new instantiation method: Read configuration from configuration file to complete instantiation

1 obj1 = mysql.from_conf ()2 obj1.tell_info ()
1 Obj.func ()
>>> not binding with anyone 2mysql.func ()
>>> not bind to anyone 3print(obj.func)
>>><function Mysql.func at 0x000002b7df688a60>4print(mysql.func)
>>><function Mysql.func at 0x000002b7df688a60>

Python binding methods and non-binding methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.