Python OO and PythonOO

Source: Internet
Author: User

Python OO and PythonOO

I want to learn Java at school in my sophomore year,

The most awesome OO thought was finished on three pages, or from Tsinghua University Press.

Later, I learned what OO is based on my own video and code.

 

Now, you can learn Python in your own way:

OO is basically encapsulation, inheritance, and polymorphism.

 

The first is inheritance:

Define a class:

Python code
  1. Class Bird (object ):
  2. Have_feather = True
  3. Way_of_reproduction = 'egg'

 

Call this class:

Python code
  1. Summer = Bird ()
  2. Print summer. way_of_reproduction

Unlike Java, Python does not need new to instantiate classes.

 

Similarly, Python classes can be set as follows:

Python code
  1. Class Bird (object ):
  2. Have_feather = True
  3. Way_of_reproduction = 'egg'
  4. Def say (self, word = 'hihi '):
  5. Print 'I say:' + word

 

Note that all class functions must have at least one parameter, which must be self.

Functions other than classes do not have this restriction.

Python code
  1. Chk = Chicken ()
  2. Print chk. have_feather
  3. Print chk. sat ('hello ')

 

_ Init _ () method

_ Init _ () is a special method (special method ). Python has some special methods. Python will process them in a special way. The name of a special method is characterized by two underscores.

The special feature of the _ init _ () method is that if you define this method in the class, once you create an object based on this class, python will automatically call this method (this process is also called initialization ).

For example:

Python code
  1. Class happyBird (Bird ):
  2. Def _ init _ (self, more_words ):
  3. Print 'we are happy birds. ', more_words
  4. Hb = happyBird ('Happy, Happy! ')

 

Overload of the parent method:

Python code
  1. Class Hello (object ):
  2. Name = 'hello'
  3. Def _ init _ (self ):
  4. Self. name = 'My name is hello'
  5. # The parameters in the class must contain the self Parameter
  6. Def sayhi (self ):
  7. Print 'Hi you'
  8. Class World (Hello ):
  9. Def _ init _ (self ):
  10. # The variable name initialized by the parent class is accessed here.
  11. Print 'before: ', Hello. name
  12. Super (World, self). _ init __()
  13. # Because the initialization constructor of the parent class is called, the variable changes of the parent class are inherited.
  14. Print 'after: ', self. name
  15. # Similar to method overload
  16. Def sayhi (self, word = 'baby '):
  17. # Call the sayhi method of the parent class
  18. Super (World, self). sayhi ()
  19. Print 'Hi' + word
  20. Def sayWorld (self ):
  21. Print 'Hi, hello world'
  22. If _ name _ = '_ main __':
  23. C = World ()
  24. C. sayhi ()
  25. C. sayWorld ()

 

In addition, python allows multi-inheritance, but this is a very dangerous operation. We recommend that you do not use it at will.

 

For Python polymorphism, just like JavaScript, you can directly access object attributes without using interfaces or type conversion.

For the type judgment, there are the type () function, andIsinstance ()Determines whether a function is a subclass.

Python code
  1. Isinstance (object, classinfo)
  2. Determines whether the instance is a class or an object is a variable.
  3. Classinfo is a type (tuple, dict, int, float)
  4. Determines whether the variable is of this type.
  5. Class objA:
  6. Pass
  7. A = objA ()
  8. B = 'A', 'V'
  9. C = 'a string'
  10. Print isinstance (A, objA)
  11. Print isinstance (B, tuple)
  12. Print isinstance (C, basestring)
  13. Output result:
  14. True
  15. True
  16. True

Python programming ideas

Write the code. This is easy without python implementation. First, there is a sendto in your personal folder, where a link is created to point to the folder you want to send.

Select a file, right-click, and select send. In the send list, select the folder you want to send.

Of course, if you use python to implement it, to be honest, the effect is too low, and you will feel that every time you press the button, there is a delay. The solution is to use pyhook to intercept all the mouse buttons and lift them up. Then, a message hook function is embedded in the desktop program (DLL injection is required). When you find that the left mouse button is lifted and the view in the desktop program contains the selected file, A menu is displayed, prompting the folder to which the user is sent.

Of course, you can also use the shortcut key and other features. I like macroexpress. Execute a macro to restrict its validity only in the desktop window. When the selected ITEM is not empty, move the ITEM to a specified folder.

However. Only the method I mentioned at the beginning is the best. Simple, stable, fast, and flexible.

Which of the following recommended books to learn about PYTHON?

Download.chinaitlab.com/program/files/17311.html

There are not many books in the Python world, but each book has its own characteristics. This is also true for Core Python. This is a book that explains the Python language in a very comprehensive way, and is not the kind of full-account model. It is truly easy to understand, and can be obtained from new beginners and veterans. If you are a newbie, you don't have to read all the content in the book at once. After reading "Python core programming", I think you can start writing Python programs, and feel the wonders of the Python world. As an old bird, the core chapter of Python may have some knowledge you have never understood, so you can learn about it in a gentle manner. The "Advanced article" can help you feel the simplicity and strength of Python.

I am very pleased with this book. It not only introduces the OO idea, but also popularized functional programming methods. It fully introduces the characteristics of the Python language. For those who want to know functional programming, it is very effective to get started. I am also trying to use this book as a teaching material to open a door for students to enter the Unix programming world! Let the students know the world of programming and the shining pearl of Python, laying the foundation for them to better learn programming in the future. All in all, I recommend this book to anyone who wants to fully learn Python.

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.