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>