【问题标题】:Class Attributes and Instance Attributes类属性和实例属性
【发布时间】:2020-12-08 01:48:14
【问题描述】:

我正在尝试学习实例属性与类属性和属性之间的区别。我有下面的代码,我试图区分这些因素。

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 attributesclass attributes 是什么。

【问题讨论】:

  • 您已经创建了一堆类属性,这些属性是您在类作用域中定义的,您立即使用__init__ 中的实例属性对其进行影射。使这些类属性变得毫无意义。
  • @juanpa.arrivillaga 我是编码新手,我该怎么办?
  • 只是不定义类属性?取决于你。顺便说一句,不要在 Python 中编写 getter 和 setter。它们没有任何用处
  • @juanpa.arrivillaga 我编写了 getter 和 setter 以使代码更具可读性,但谢谢!我的主要问题是在这种情况下class attributesinstance attributesattributes 是什么。如何区分它们?

标签: python function oop attributes instance-variables


【解决方案1】:

我正在尝试学习实例属性与类属性和属性之间的区别。

应该有两个属性,class attributeinstance attribute。或instance attribute&none-instance attribute 方便。

instance attribute

  • 只有在调用 __init__ 时才会激活这些内容。
  • 只有在 Class 初始化后才能访问 nm,通常为 self.xxx
  • 以及以self为第一个参数的类中的方法(通常),这些函数是实例方法,只有在初始化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 attributestatic attribute,随便你叫什么

  • 这些东西与 Class 一起保持激活状态。
  • 这意味着您可以随时访问它们,例如__init__,甚至在__new__
  • Classinstance 都可以调用它们。
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

结论

“类属性”可以被实例和类调用

“实例属性”只能通过实例调用。

【讨论】:

    猜你喜欢
    • 2011-11-24
    • 2018-04-14
    • 2021-12-26
    • 2010-11-21
    • 1970-01-01
    • 2015-12-09
    • 2020-03-28
    • 2011-10-24
    相关资源
    最近更新 更多