【问题标题】:Variable shared between two instances of a class in Python [closed]Python中类的两个实例之间共享的变量[关闭]
【发布时间】:2018-12-11 15:15:59
【问题描述】:

不使用继承,我们可以在一个类的两个实例之间共享一个变量吗? 问题是变量需要 3.5GB。使用继承不是一个好的选择,因为我可能会也可能不会加载这个变量。

【问题讨论】:

  • 你可以对内存中的同一个对象有多个引用。这与继承本身无关。
  • 我不是很懂你,你指的是哪个内存?您的意思是将变量从第一个实例传递给第二个实例吗?
  • 对于一个模糊的问题,我只能给出一个模糊的建议。你到底想做什么?
  • 为什么不能这样做,为什么不能有 big_obj = "blah" 然后 x=Go_DB(big_obj) y=Go_DB(big_obj)?我不确定我是否正确理解这里的问题。
  • @hygull 确实如此。谢谢。

标签: python python-3.x oop inheritance


【解决方案1】:

您可以为此使用类变量。请看下面一个家庭共享一个帐户的简单示例。

https://www.tutorialspoint.com/python/python_classes_objects.htm

class FamilyAccount(object):
    total_amount = 0 # class variable

    def __init__(self, fullname, age):
        self.fullname = fullname # instance variable
        self.age = age           # instance variable

    def credit(self, amount):
        FamilyAccount.total_amount += amount

    def debit(self, amount):
        FamilyAccount.total_amount -= amount

    def details(self):
        print("Fullname: ", self.fullname)
        print("Age     : ", self.age)
        print("Amount  : ", self.total_amount)
        print() # new line

# Create account for Rishikesh Agrawani
account1 = FamilyAccount("Rishikesh Agrawani", 26)
# Rishikesh Agrawani credits 1000
account1.credit(1000)
# Print account information of Rishikesh Agrawani
account1.details() # 1000


# Create account for Shay Banon
account2 = FamilyAccount("Shay Banon", 30)
# Shay Banon debits 500
account2.debit(500)
# Print account information of Shay Banon
account2.details() # 500

# Print account information of Rishikesh Agrawani (again)
account1.details() # 500

# Fullname:  Rishikesh Agrawani
# Age     :  26
# Amount  :  1000

# Fullname:  Shay Banon
# Age     :  30
# Amount  :  500

# Fullname:  Rishikesh Agrawani
# Age     :  26
# Amount  :  500

如果你想用10而不是0来初始化类变量,你可以修改上面的代码如下。

class FamilyAccount(object):
    total_amount = 0 # class variable

    def __init__(self, fullname, age, amount = 0, set_amount = False):
        if set_amount: 
            # If you want to reset amount for all members
            # total_amount will be re-intialized (visible to all instances)
            FamilyAccount.total_amount = amount

        self.fullname = fullname # instance variable
        self.age = age           # instance variable

    def credit(self, amount):
        FamilyAccount.total_amount += amount

    def debit(self, amount):
        FamilyAccount.total_amount -= amount

    def details(self):
        print("Fullname: ", self.fullname)
        print("Age     : ", self.age)
        print("Amount  : ", self.total_amount)
        print() # new line

# Create account for Rishikesh Agrawani
account1 = FamilyAccount("Rishikesh Agrawani", 26, 10, set_amount = True)
# Rishikesh Agrawani credits 1000
account1.credit(1000)
# Print account information of Rishikesh Agrawani
account1.details() # 1010


# Create account for Shay Banon
account2 = FamilyAccount("Shay Banon", 30)
# Shay Banon debits 500
account2.debit(500)
# Print account information of Shay Banon
account2.details() # 510

# Print account information of Rishikesh Agrawani (again)
account1.details() # 510

# Fullname:  Rishikesh Agrawani
# Age     :  26
# Amount  :  1010

# Fullname:  Shay Banon
# Age     :  30
# Amount  :  510

# Fullname:  Rishikesh Agrawani
# Age     :  26
# Amount  :  510

【讨论】:

  • @hyugull 只是我在这里遇到的一个小问题,假设我需要传递一个值来将total_amount 初始化为account1 的某个值,可以这样做吗?因为这超出了__init__ 的范围。
  • 是的,只需对现有代码进行少量修改即可完成。我正在添加代码 sn-p the same example。请检查。这里我使用了默认参数的概念。谢谢。
  • set_amount 决定是否要重置金额。如果你不检查它,你会得到不需要的结果,因为它会被所有实例化 (object creations) 重置,并且我们知道类变量是由同一类的所有实例共享的。
  • 我们不需要像amount = 0set_amount = False这样传递默认参数(关键字参数)的值,在这种情况下,将采用默认值,如果您愿意,可以通过传递我在创建第一个帐户 account1 时传递的值。
  • 你拯救了我的一天。是的,我可以阅读代码。我只需要在第一个实例创建中标记并加载 3.5GB 数据。
猜你喜欢
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
  • 2019-01-30
相关资源
最近更新 更多