10 python-side question traps to see if you can recruit

Source: Internet
Author: User

Question 1:How can I modify the following Python code so that the following code calls the Show method of Class A?

Answer: The test center of this problem is class inheritance, as long as the __class__ method to specify the class object can be. The following code is added:

Question 2: How can I modify the following Python code to make the code work?

Answer: This question is a method object, in order to let the object instance can be called directly, need to implement the __call__ method, the supplementary code is as follows:

Question 3: What is the output of the following code?

For:

This topic examines the use of new and INIT, and using the __new__ method, you can decide to return that object, which is called before the object is created, a singleton, factory pattern that is common in design mode. __init__ is called to create an object.

Question 4: What does the following code output?

For:

This topic examines the generation of lists and dictionaries.

Question 5: What does the following code output?

For:

This topic examines global variables and local variables. Num is not a global variable, so each function has its own num copy, and if you want to modify NUM, you must declare it with the global keyword. Like the following.

6: How do I exchange two variable values using one line of code?

For:

Question 7: How do I add code so that no defined method calls the Mydefault method?

For:

The test of this question is the default method of Python, which calls method __getattr__ only if there are no method calls defined. When the Fn1 method passes in the parameter, we can add a *args indeterminate parameter to the Mydefault method to be compatible.

Question 8: There are three modules in a package, mod1.py, mod2.py, mod3.py, but how to ensure that only mod1 and mod3 are imported when using the From demopack import * module.

A: Add the __init__.py file to the package and add it to the file:

Question 9: write a function, receive integer parameter n, return a function, function return n and the product of the parameter.

For:

question: What is the hidden danger of the following code? (In Python2)

A: Because the variable str is an immutable object, each iteration, Python generates a new STR object to store the new string, and the larger the NUM, the more the STR objects are created, and the memory consumption becomes larger.

This article is supported by Marco Education Python learners, and if you are having trouble learning Python, please contact me via qq:2282011279 or phone: 15652199186.

10 python-side question traps to see if you can recruit

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.