Class 属性和实例属性
Class Attributes and Instance Attributes
我正在尝试了解实例属性与class属性和属性之间的区别。我有下面的代码,我正在尝试区分这些因素。
class Student:
firstname = ""
lastname = ""
ucid = ""
department = ""
nationality = ""
courses = {}
def __init__(self, fname, lname, ucid, dept, n):
self.firstname = fname
self.lastname = lname
self.ucid = ucid
self.department = dept
self.nationality = n
self.courses = {}
def setName(self, fname, lname):
self.firstname = fname
self.lastname = lname
def setDepartment(self, d):
self.department = d
def setUcid(self, u):
self.ucid = u
def setNationality(self, n):
self.nationality = n
def addCourse(self, coursename, gpa):
self.courses[coursename] = gpa
def printAll(self):
print("The name of the student is ", self.firstname, self.lastname)
print("nationality and UCID: ", self.nationality, self.ucid)
print("Department: ", self.department)
print("Result: ")
for key in self.courses.keys():
print(key, self.courses[key])
print("--------------------\n")
s1=Student("Beth","Bean","30303","Computer Science","International")
s1.addCourse("SCIENCE",3.75)
s1.printAll()
s2=Student("Mac","Miller","30303","Envr Science","American")
s2.addCourse("MATH",4.00)
s2.printAll()
据我了解,属性是:firstname,lastname,ucid,department,nationality,courses
但我不知道 instance attributes
和 class attributes
是什么。
I am trying to learn the difference between the instance attributes and class attributes and attributes.
应该有两个属性,class attribute
,instance attribute
。或 instance attribute
&none-instance attribute
为了方便起见。
instance attribute
- 这些是仅在调用
__init__
时才会激活的东西。
- 只有在Class初始化后才能访问thenm,一般见
self.xxx
.
- 和class中的方法以
self
为第一个参数(通常),这些函数是实例方法,只有在初始化Class后才能访问。
- 和class中的方法与
@property
deco,它们是实例属性
common seen instance attribute
class Name(object):
def __init__(self):
self.age = 100
def func(self):
pass
@property
def age(self):
return self.age
class attribute
non-instance attribute
或 static attribute
,无论你怎么称呼它
- 这些东西与 Class 一起保持激活状态。
- 这意味着您可以随时访问它们,例如
__init__
,即使在 __new__
.
- 它们可以被
Class
和 instance
调用。
common seen class attribute
class Name(object):
attr = 'Im class attribute'
还有一些你可能应该知道的东西,class method
,它与 Class 一起保持激活,但不同之处在于 class method
不能被实例调用,只能被 Class。这里的例子
class Name(object)
attr = 'Im class attribute'
@classmethod
def get_attr(cls):
return cls.attr
结论
"class属性"可以被实例和Class
调用
“实例属性”只能被实例调用。
我正在尝试了解实例属性与class属性和属性之间的区别。我有下面的代码,我正在尝试区分这些因素。
class Student:
firstname = ""
lastname = ""
ucid = ""
department = ""
nationality = ""
courses = {}
def __init__(self, fname, lname, ucid, dept, n):
self.firstname = fname
self.lastname = lname
self.ucid = ucid
self.department = dept
self.nationality = n
self.courses = {}
def setName(self, fname, lname):
self.firstname = fname
self.lastname = lname
def setDepartment(self, d):
self.department = d
def setUcid(self, u):
self.ucid = u
def setNationality(self, n):
self.nationality = n
def addCourse(self, coursename, gpa):
self.courses[coursename] = gpa
def printAll(self):
print("The name of the student is ", self.firstname, self.lastname)
print("nationality and UCID: ", self.nationality, self.ucid)
print("Department: ", self.department)
print("Result: ")
for key in self.courses.keys():
print(key, self.courses[key])
print("--------------------\n")
s1=Student("Beth","Bean","30303","Computer Science","International")
s1.addCourse("SCIENCE",3.75)
s1.printAll()
s2=Student("Mac","Miller","30303","Envr Science","American")
s2.addCourse("MATH",4.00)
s2.printAll()
据我了解,属性是:firstname,lastname,ucid,department,nationality,courses
但我不知道 instance attributes
和 class attributes
是什么。
I am trying to learn the difference between the instance attributes and class attributes and attributes.
应该有两个属性,class attribute
,instance attribute
。或 instance attribute
&none-instance attribute
为了方便起见。
instance attribute
- 这些是仅在调用
__init__
时才会激活的东西。 - 只有在Class初始化后才能访问thenm,一般见
self.xxx
. - 和class中的方法以
self
为第一个参数(通常),这些函数是实例方法,只有在初始化Class后才能访问。 - 和class中的方法与
@property
deco,它们是实例属性
common seen instance attribute
class Name(object):
def __init__(self):
self.age = 100
def func(self):
pass
@property
def age(self):
return self.age
class attribute
non-instance attribute
或 static attribute
,无论你怎么称呼它
- 这些东西与 Class 一起保持激活状态。
- 这意味着您可以随时访问它们,例如
__init__
,即使在__new__
. - 它们可以被
Class
和instance
调用。
common seen class attribute
class Name(object):
attr = 'Im class attribute'
还有一些你可能应该知道的东西,class method
,它与 Class 一起保持激活,但不同之处在于 class method
不能被实例调用,只能被 Class。这里的例子
class Name(object)
attr = 'Im class attribute'
@classmethod
def get_attr(cls):
return cls.attr
结论
"class属性"可以被实例和Class
调用“实例属性”只能被实例调用。