Python Object-oriented programming (IV)

Source: Internet
Author: User

1. polymorphic

The concept of polymorphism is now being said, but we have been using it. Polymorphism is the meaning of many forms, animals are cats, dogs, pigs and so on, these are the various forms of animals.

Reflected in Python, polymorphism means that you can manipulate a variable even if you don't know what type of object it refers to. For example, the sequence type has the method of calculating the length Len (), then we get a sequence type X, we do not need to know what the X is, we just need to know that it is a sequence type, then we can use Len (x) method to calculate the length of x. This is polymorphism.

Of course we've learned a built-in function, Isinstance (), which can be used to determine the data type, but it doesn't fit the polymorphic nature.

2. Binding methods and non-binding methods

There are two types of functions defined within a class, binding methods and non-binding methods

1) Binding method

Binding methods are also divided into methods that are bound to classes and methods that are bound to objects.

Any method that is defined in a class and is not decorated by any adorner is the method used to bind to the object.

The feature is that Obj.func () automatically passes obj as the first parameter, because the logic of Func is to process the obj
The method defined in the class that is decorated by the Classmethod adorner is the method that binds to the class

The feature is that Cls.func () automatically passes in the class CLS as the first parameter, because the logic of Func is to process the CLS, even if the object calls this function, and the class is passed in as the first argument

classpeople:def __init__(self,name): Self.name=namedefBar (self):Print('----->', Self.name) @classmethoddeffunc (CLS):Print(CLS) P1=people ('Zhang') P1.func ()#<class ' __main__. People ' >People.func ()#<class ' __main__. People ' >

2) Non-binding method

There is also a method that is neither bound to a class nor bound to an object, called a non-binding method

Decorate with a staticmethod decorator

#file name: Pickle_testImportHashlibImport TimeImportPickleImportOsstudent_path=r'C:\Users\Administrator\PycharmProjects\python5 period \day22\student'classpeople:def __init__(self,name,sex,user_id): Self.name=name Self.sex=Sex self.user_id=user_id self.id=self.create_id ()defTell_info (self):Print(" "--------%s Info--------id:%s name:%s sex:%s user_id:%s" "%(self.name,self.id,self.name,self.sex,self.user_id))defcreate_id (self): M=hashlib.md5 () m.update (Self.name.encode ('Utf-8')) M.update (Self.sex.encode ('Utf-8')) m.update (str (self.user_id). Encode ('Utf-8'))        returnm.hexdigest ()defSave (self): with open (Self.id,'WB') as F:pickle.dump (self,f) @staticmethod#a non-binding method is a function, which is a tool that does not require classes or objects    defGet_all (): Res=Os.listdir (Student_path) forIteminchRes:file_path=r'%s\%s'%(Student_path,item) with open (File_path,'RB') as F:obj=pickle.load (f) obj.tell_info ()#deserialization. PY fromPickle_testImportPeoplep=people ('Egon','male', 123123123) P.get_all ()

3. Software Development Specification

In the real software development process, not to write all the code in a file, you can imagine, a small program about 10000 lines of code, are written in a file, you still do this line?

The right way should be to split the program into a single module. such as the common Bin directory to put executable files, conf directory put configuration files

DB directory to put the data, log directory files, lib directory repository, SRC directory put some important code, such as important logic, class definition, etc.

Python Object-oriented programming (IV)

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.