Python程式設計 10個面試常問的問題

2021-08-14 02:18:04 字數 4518 閱讀 6374

def

class_test

():class

a(object):

defshow

(self):

print("class a")

defhello

(self):

print("hello")

class

b(a):

defshow

(self):

print("class b")

b =b()

b.show()

b.hello()

# 呼叫類a的show方法

b.__class__ = a

b.show()

# __class__方法指向了類物件,只用給他賦值型別a,

# 然後呼叫方法show,但是用完了記得修改回來。

"""out

class b

hello

class a

"""

def

call_test

():class

a(object):

def__init__

(self, a, b):

self._a = a

self._b = b

defmyprint

(self):

print("a =", self._a, "b =", self._b)

# 為了能讓物件例項能被直接呼叫,需要實現__call__方法

def__call__

(self, num):

print("call:", num)

a = a(1, 2)

a.myprint()

a(11)

"""out:

a = 1 b = 2

call: 11

"""

def

new_test

():class

a(object):

def__init__

(self):

print("a init")

deffoo(self):

print("a foo")

class

b(object):

def__init__

(self, a):

print("b init")

# 使用__new__方法,可以決定返回那個物件,也就是建立物件之前,

# 這個可以用於設計模式的單例、工廠模式。

# __init__是建立物件是呼叫的。

def__new__

(cls, a):

print("b new")

if a>10:

return super(b, cls).__new__(cls)

return a()

deffoo(self):

print("b foo")

b1 = b(5)

b1.foo()

b2 = b(20)

b2.foo()

"""out:

b new

a init

a foo

b new

b init

b foo

"""

def

list_test

(): lst = [i for i in range(5)]

print(lst) # [0, 1, 2, 3, 4]

list1 = [i for i in lst if i >2]

print(list1) # [3, 4]

list1 = [i**2

for i in lst if i >2]

print(list1) # [9, 16]

dict1 =

print(dict1) #

dict2 =

print(dict2) #

set1 =

print(set1) #

def

global_test

(): num = 10

deff1

(): num = 20

print("f1")

deff2():

print(num)

f2() # 10

f1() # f1

f2() # 10

# num不是個全域性變數,所以每個函式都得到了自己的num拷貝,

# 如果你想修改num,則必須用global關鍵字宣告。

global n

n = 10

deff3

():global n # 說明使用全域性變數a,不說明則是區域性變數

n = 30

deff4

(): print(n)

f4() # 10

f3()

f4() # 30

def

swap_test

(): a = 1

b= 2

print("a =", a, "b =", b)

a, b = b, a # 一行**交換兩個變數值

print("a =", a, "b =", b)

def

default_test

():class

a(object):

def__init__

(self):

print("init")

# 當fn1方法傳入引數時,我們可以給mydefault方法增加乙個*args不定引數來相容。

defmydefault

(self, *args):

print("mydefault",args[0])

# 方法__getattr__只有當沒有定義的方法呼叫時,才呼叫他。

def__getattr__

(self, name):

print("name:", name)

return self.mydefault

a = a()

a.f1(0)

a.f2(1, 2)

a.f3(2, 3, 4)

# 乙個包裡有三個模組,mod1.py, mod2.py, mod3.py,

# 但使用from demopack import *匯入模組時,只有mod1、mod3被匯入了。

# 增加__init__.py檔案,並在檔案中增加:

# __all__ = ['mod1','mod3']

from demopack import * # import * only allowed at module level

defpackge_test

():pass

"""out

mod1

mod3

"""

# 函式,接收整數引數n,返回乙個函式,

# 函式的功能是把函式的引數和n相乘並把結果返回。

defclosure_test

():def

add(num):

defadd_num

(val):

return num + val

return add_num

a = add(5)

print(a(5))

def

performance_test

():import time

defstr_test

(num):

start = time.time()

string = ""

for i in range(num):

string += str(i)

end = time.time()

print("str_time:", end - start)

# print(string)

def(num):

start = time.time()

lst=

for i in range(num):

end = time.time()

# print("".join(lst))

num = 1000000

str_test(num) # str_time: 1.926110029220581

# mygod!!! 時間相差6倍

# python的str是個不可變物件,每次迭代,都會生成新的str物件來儲存新的字串

# num越大,建立的str物件越多,記憶體消耗越大。

defmain

(): performance_test()

if __name__ == '__main__':

main()

10個Python面試常問的問題

有如下的一段 class a object def show self print base show class b a def show self print derived show obj b obj.show 如何呼叫類a的show方法了。方法如下 obj.class a obj.show...

10個Python面試常問的問題

概述 1 類繼承 有如下的一段 class a object def show self print base show class b a def show self print derived show obj b obj.show 如何呼叫類a的show方法了。方法如下 obj.class a...

python基礎面試常問程式設計問題

求數值以內的質數,合數及個數 質數 defzhishu number num 0for i in range 2 number 1 k true for j in range 2 i if i j continue if i j 0 k false break if k true print d i...