class Course:
language = 'Chinese'
def __init__(self,teacher,course_name,period,price):
self.teacher = teacher
self.name = course_name
self.period = period
self.price = price
def func(self):
pass
Course.language = 'English' #類的靜態屬性只能經過對象或類這種方式修改。不能經過字典
# Course.__dict__['language'] = 'Chinese'
# print(Course.language)
python = Course('egon','python','6 months',20000)
linux = Course('oldboy','linux','6 months',20000)
Course.language = 'Chinese'
print(python.language) #Chinese
print(linux.language) #Chinese
python.language = 'py'
print(Course.language) #Chinese
print(python.language) #py
print(linux.language) #Chinese
print(linux.__dict__) #查看linux對象名稱空間的名稱
print(python.__dict__) #查看python對象名稱空間的名稱
del python.language
print(python.__dict__)
class Course:
language = ['Chinese']
def __init__(self,teacher):
self.teacher = teacher
python = Course('egon') #['Chinese']
linux = Course('oldboy') #['Chinese']
python.language[0] = 'English'
print(Course.language) #['English']
python = Course('egon') #['English']
linux = Course('oldboy') #['English']
python.language = ['English'] #這樣卻不會修改類屬性,由於這是創建一個新列表,一個新地址,和原列表不要緊。
python = Course('egon')
linux = Course('oldboy')
def func():pass
print(func)
class Foo:
def func(self):
print('func')
def fun1(self):
pass
f1 = Foo()
print(Foo.func) #除了對象,其它的都不能和func造成綁定關係。
print(f1.func) #f1.func就是將f1與func方法綁定
# print(f1.fun1)
#<bound method Foo.func of f1>