[英]Class Attributes and Instance Attributes
I am trying to learn the difference between the instance attributes and class attributes and attributes.我正在尝试了解实例属性与 class 属性和属性之间的区别。 I have the code below and I am trying to distinguish these factors.
我有下面的代码,我试图区分这些因素。
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()
From what I understood the attributes would be: firstname,lastname,ucid,department,nationality,courses
But I do not know what instance attributes
and class attributes
would be.据我了解,属性将是:
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 属性和属性之间的区别。
there should be two attributes, class attribute
, instance attribute
.应该有两个属性,
class attribute
, instance attribute
。 or instance attribute
& none-instance attribute
for convenience.或
instance attribute
和none-instance attribute
为方便起见。
instance attribute
__init__
has been called.__init__
时才激活的东西。self.xxx
.self.xxx
。self
as its first parameter(normally), these functions are instance methods, and you can only access after you initialized the Class.self
(通常),这些函数是实例方法,只有在初始化Class后才能访问。@property
deco, they are instance attributes@property
装饰的方法,它们是实例属性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
or static attribute
, whatever you call it non-instance attribute
或static attribute
,不管你怎么称呼它
__init__
, even in __new__
.__init__
,甚至在__new__
中。Class
and instance
.Class
和instance
调用。common seen class attribute
class Name(object):
attr = 'Im class attribute'
there is something else you may should know, class method
, which stay activated along with Class but the difference is class method
can't be called by instance but only Class.还有一些你可能应该知道的,
class method
,它与 Class 一起保持激活,但区别是class method
不能被实例调用,而只能调用 Class example here这里的例子
class Name(object)
attr = 'Im class attribute'
@classmethod
def get_attr(cls):
return cls.attr
"class attribute" can be called by both instance and Class实例和 Class 都可以调用“类属性”
"instance attribute" can only called by instance. “实例属性”只能通过实例调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.