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?
- Class A (Object)
- Def show (self):
- print ' derived show '
- Class B (A)
- Def show (self):
- print ' derived show '
- Obj=b ()
- 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:
- Obj._class_=a
- Obj.show ()
Copy Code
Question 2: How can I modify the following Python code to make the code work?
- Class A (object):
- def _init_ (self,a,b):
- Self._a = A
- Self._b = b
- def myprint (self):
- print ' a= ', self._a, ' b= ', self._b
- A1=a (10,20)
- A1.myprint ()
- 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:
- Class A (object):
- def _init_ (self,a,b):
- Self._a = A
- Self._b = b
- def myprint (self):
- print ' a= ', self._a, ' b= ', self._b
- Def_call_ (Self,num):
- print ' Call: ', num+self._a
Copy Code
Question 3: What is the output of the following code?
- Class B (object):
- DEF fn (self):
- Print "B FN"
- Def_init_ (self):
- Print "B INIT"
- Class A (object):
- DEF fn (self):
- Print "A FN"
- Def_new_ (cls,a):
- Print "NEW", a
- If a>10:
- Return Super (A,CLS). _new_ (CLS)
- Return B ()
- Def_init_ (self,a):
- Print "INIT", a
- A1=a (5)
- A1,FN ()
- A2=a (20)
- A2,FN ()
Copy Code
For:
- NEW 5
- B INIT
- B fn
- NEW 20
- INIT 20
- 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?
- 1s=[1,2,3,4]
- List1 =[i for i in LS if i>2
- Print List1
- List2 =[1*2 for i in LS if 1>2
- Print List2
- Dicl={x:x**2 for X in (2, 4, 6)}
- Print Dic1
- Dic2={x: ' Item ' + str (x**2) for x in (2, 4, 6)}
- Print Dic2
- Setl ={x for X "Hello World" if x not in ' Low level '}
- Print Set1
Copy Code
For:
- [3,4]
- [6,8]
- {2:4,4:16,6:36}
- {2: ' Item4 ', 4: ' Item16 ', 6: ' Item36 '}
- Set (["H", ' R ', ' d "])
Copy Code
This topic examines the generation of lists and dictionaries.
Question 5: What does the following code output?
- Num= 9
- Def f1 ():
- Um=20
- def f2 ():
- Print num
- F2 ()
- F1 ()
- F2 ()
Copy Code
For:
- 9
- 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.
- Num=9
- Def f1 ():
- Global num
- Um=20
- def f2 ():
- Print num
- F2 ()
- F1 ()
- F2 ()
- #prints:
- #9
- #20
Copy Code
6: How do I exchange two variable values using one line of code?
- A=8
- B=9
Copy Code
For:
- (A, b) = (b,a)
Copy Code
Question 7: How do I add code so that no defined method calls the Mydefault method?
- Class A (object):
- Def_init_ (SELF,A,B):
- Self.a1=a
- Self.b1=b
- print ' init '
- def mydefault (self):
- print ' Default '
- A1=a (10,20)
- A1.FN1 ()
- A1.FN2 ()
- A1.fn3 ()
Copy Code
For:
- Class A (object):
- Def_init_ (SELF,A,B):
- Self.a1=a
- Self.b1=b
- print ' init '
- def mydefault (self):
- print ' Default '
- Def_getattr_ (Self,name):
- Return Self.mydefault
- A1=a (10,20)
- A1.FN1 ()
- A1.FN2 ()
- 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.
- Class A (object):
- Def_init_ (SELF,A,B):
- Self.a1=a
- Self.b1=b
- print ' init '
- def mydefault (Self,*args):
- print ' Default: ' +str (Args[0])
- Def_getattr_ (Self,name):
- Print "Other fn:", name
- Return Self.mydefault
- A1=a (10,20)
- A1.FN1 (33)
- A1.fn2 (' Hello ')
- 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:
- _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:
- def mulby (num):
- DEF GN (val):
- Return Num*val
- return GN
- Zw=mulby (7)
- Print (ZW (9));
Copy Code
Question 10: What is the hidden danger of the following code? (In Python2)
- def strtest1 (num):
- Str= ' first '
- For I in range (num):
- str+= "X"
- 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