简体   繁体   English

Class 属性和实例属性

[英]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 attributesclass 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 attributeinstance attribute or instance attribute & none-instance attribute for convenience.instance attributenone-instance attribute为方便起见。

instance attribute

  • these are things activated only when __init__ has been called.这些是仅在调用__init__时才激活的东西。
  • you can only access thenm after Class is initialized, which commonly seen as self.xxx .必须在 Class 初始化后才能访问 nm,一般看为self.xxx
  • and methods in class with self as its first parameter(normally), these functions are instance methods, and you can only access after you initialized the Class.和class中的方法,第一个参数是self (通常),这些函数是实例方法,只有在初始化Class后才能访问。
  • and methods in class with @property deco, they are instance attributes和 class 中带有@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 attributestatic attribute ,不管你怎么称呼它

  • these things stay activated along with Class.这些东西与 Class 一起保持激活状态。
  • which means you can access them whenever you need to, like __init__ , even in __new__ .这意味着您可以在需要时访问它们,例如__init__ ,甚至在__new__中。
  • they can be called by both Class and instance .它们可以被Classinstance调用。
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

Conclusion结论

"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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM