Python中一个类的两个实例之间共享的变量
如果不使用继承,我们可以在一个类的两个实例之间共享一个变量吗?Python中一个类的两个实例之间共享的变量,python,python-3.x,oop,inheritance,Python,Python 3.x,Oop,Inheritance,如果不使用继承,我们可以在一个类的两个实例之间共享一个变量吗? 问题是变量需要3.5GB。使用继承不是一个好的选择,因为我可能会也可能不会加载此变量。您可以使用类变量进行此操作。请看下面的简单示例,其中一个家庭共享一个帐户 如果您想使用0以外的其他值(如10)初始化类变量,可以按如下方式修改上述代码 您可以为此使用类变量。请看下面的简单示例,其中一个家庭共享一个帐户 如果您想使用0以外的其他值(如10)初始化类变量,可以按如下方式修改上述代码 可以在内存中对同一对象进行多个引用。这与继承本身
问题是变量需要3.5GB。使用继承不是一个好的选择,因为我可能会也可能不会加载此变量。您可以使用类变量进行此操作。请看下面的简单示例,其中一个家庭共享一个帐户 如果您想使用
0
以外的其他值(如10
)初始化类变量,可以按如下方式修改上述代码
您可以为此使用类变量。请看下面的简单示例,其中一个家庭共享一个帐户 如果您想使用
0
以外的其他值(如10
)初始化类变量,可以按如下方式修改上述代码
可以在内存中对同一对象进行多个引用。这与继承本身无关。我不太理解你,你指的是哪种记忆?你的意思是将变量从第一个实例传递到第二个实例吗?我只能对一个模糊的问题给出一个模糊的建议。你到底想做什么?为什么你不能这样做,为什么你不能有big_obj=“blah”然后x=Go_DB(big_obj)y=Go_DB(big_obj)?我不确定我是否正确理解了问题所在。谢谢。您可以在内存中对同一对象进行多次引用。这与继承本身无关。我不太理解你,你指的是哪种记忆?你的意思是将变量从第一个实例传递到第二个实例吗?我只能对一个模糊的问题给出一个模糊的建议。你到底想做什么?为什么你不能这样做,为什么你不能有big_obj=“blah”然后x=Go_DB(big_obj)y=Go_DB(big_obj)?我不确定我是否正确理解了问题所在。谢谢。@hyugull这里有一个小问题,假设我需要传递一个值来初始化
total\u amount
到account1
的某个值,可以这样做吗?因为这超出了\uuuu init\uuuu
的范围。是的,只需对现有代码进行少量修改即可完成。我正在添加相同示例的代码片段。请查收。这里我使用了默认参数的概念。谢谢。设置金额
决定是否重置金额。如果您不检查它,您将得到不想要的结果,因为它将被所有实例化重置(对象创建)
,并且我们知道类变量由同一类的所有实例共享。我们不需要传递默认参数(关键字参数)的值,如amount=0
,set\u amount=False
,在这种情况下,将采用默认值,如果愿意,您可以通过在创建第一个帐户account1
时传递我传递的值来覆盖。您保存了我的一天。是的,我能读懂密码。我只需要在第一个实例创建中标记并加载3.5GB数据。@hyugull这里有一个小问题,假设我需要传递一个值来初始化total\u amount
到account1
的某个值,可以这样做吗?因为这超出了\uuuu init\uuuu
的范围。是的,只需对现有代码进行少量修改即可完成。我正在添加相同示例的代码片段。请查收。这里我使用了默认参数的概念。谢谢。设置金额
决定是否重置金额。如果您不检查它,您将得到不想要的结果,因为它将被所有实例化重置(对象创建)
,并且我们知道类变量由同一类的所有实例共享。我们不需要传递默认参数(关键字参数)的值,如amount=0
,set\u amount=False
,在这种情况下,将采用默认值,如果愿意,您可以通过在创建第一个帐户account1
时传递我传递的值来覆盖。您保存了我的一天。是的,我能读懂密码。我只需要在第一个实例创建中标记并加载3.5GB数据。
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
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