Python interview basic question ten traps, did you recruit?

Source: Internet
Author: User

We will meet a variety of interviews, some even the HR specifically for you to set the barrier, in the Python interview is also, whether you are candidates for Python web development, crawler engineers, or data analysis, or automated operations, these Python interview basic Questions ten traps Maybe you will encounter, Today's Python training is summed up in case you're ready to receive!
Question 1: How can I modify the following Python code so that the following code calls the Show method of Class A?

    1. Class A (Object)
    2. Def show (self):
    3. print ' derived show '
    4. Class B (A)
    5. Def show (self):
    6. print ' derived show '
    7. Obj=b ()
    8. Obj.show ()

Copy Code

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:

    1. Obj._class_=a
    2. Obj.show ()

Copy Code

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

    1. Class A (object):
    2. def _init_ (self,a,b):
    3. Self._a = A
    4. Self._b = b
    5. def myprint (self):
    6. print ' a= ', self._a, ' b= ', self._b
    7. A1=a (10,20)
    8. A1.myprint ()
    9. a1= (80)

Copy Code

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:

    1. Class A (object):
    2. def _init_ (self,a,b):
    3. Self._a = A
    4. Self._b = b
    5. def myprint (self):
    6. print ' a= ', self._a, ' b= ', self._b
    7. Def_call_ (Self,num):
    8. print ' Call: ', num+self._a

Copy Code

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

    1. Class B (object):
    2. DEF fn (self):
    3. Print "B FN"
    4. Def_init_ (self):
    5. Print "B INIT"
    6. Class A (object):
    7. DEF fn (self):
    8. Print "A FN"
    9. Def_new_ (cls,a):
    10. Print "NEW", a
    11. If a>10:
    12. Return Super (A,CLS). _new_ (CLS)
    13. Return B ()
    14. Def_init_ (self,a):
    15. Print "INIT", a
    16. A1=a (5)
    17. A1,FN ()
    18. A2=a (20)
    19. A2,FN ()

Copy Code

For:

    1. NEW 5
    2. B INIT
    3. B fn
    4. NEW 20
    5. INIT 20
    6. A fn

Copy Code

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?

    1. 1s=[1,2,3,4]
    2. List1 =[i for i in LS if i>2
    3. Print List1
    4. List2 =[1*2 for i in LS if 1>2
    5. Print List2
    6. Dicl={x:x**2 for X in (2, 4, 6)}
    7. Print Dic1
    8. Dic2={x: ' Item ' + str (x**2) for x in (2, 4, 6)}
    9. Print Dic2
    10. Setl ={x for X "Hello World" if x not in ' Low level '}
    11. Print Set1

Copy Code

For:

    1. [3,4]
    2. [6,8]
    3. {2:4,4:16,6:36}
    4. {2: ' Item4 ', 4: ' Item16 ', 6: ' Item36 '}
    5. Set (["H", ' R ', ' d "])

Copy Code

This topic examines the generation of lists and dictionaries.
Question 5: What does the following code output?

    1. Num= 9
    2. Def f1 ():
    3. Um=20
    4. def f2 ():
    5. Print num
    6. F2 ()
    7. F1 ()
    8. F2 ()

Copy Code

For:

    1. 9
    2. 9

Copy Code

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.

    1. Num=9
    2. Def f1 ():
    3. Global num
    4. Um=20
    5. def f2 ():
    6. Print num
    7. F2 ()
    8. F1 ()
    9. F2 ()
    10. #prints:
    11. #9
    12. #20

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

    1. A=8
    2. B=9

Copy Code

For:

    1. (A, b) = (b,a)

Copy Code

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

    1. Class A (object):
    2. Def_init_ (SELF,A,B):
    3. Self.a1=a
    4. Self.b1=b
    5. print ' init '
    6. def mydefault (self):
    7. print ' Default '
    8. A1=a (10,20)
    9. A1.FN1 ()
    10. A1.FN2 ()
    11. A1.fn3 ()

Copy Code

For:

    1. Class A (object):
    2. Def_init_ (SELF,A,B):
    3. Self.a1=a
    4. Self.b1=b
    5. print ' init '
    6. def mydefault (self):
    7. print ' Default '
    8. Def_getattr_ (Self,name):
    9. Return Self.mydefault
    10. A1=a (10,20)
    11. A1.FN1 ()
    12. A1.FN2 ()
    13. A1.fn3 ()

Copy Code

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.

    1. Class A (object):
    2. Def_init_ (SELF,A,B):
    3. Self.a1=a
    4. Self.b1=b
    5. print ' init '
    6. def mydefault (Self,*args):
    7. print ' Default: ' +str (Args[0])
    8. Def_getattr_ (Self,name):
    9. Print "Other fn:", name
    10. Return Self.mydefault
    11. A1=a (10,20)
    12. A1.FN1 (33)
    13. A1.fn2 (' Hello ')
    14. A1.FN3 (10)

Copy Code
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:

    1. _all_=[' mod1 ', ' mod3 ']

Copy Code

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

    1. def mulby (num):
    2. DEF GN (val):
    3. Return Num*val
    4. return GN
    5. Zw=mulby (7)
    6. Print (ZW (9));

Copy Code

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

    1. def strtest1 (num):
    2. Str= ' first '
    3. For I in range (num):
    4. str+= "X"
    5. Return str

Copy Code

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.

126 reads
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.