[英]Python 2.7 How can I access objects created by one class from a list stored within a different class
I'm trying to create a class User that stores certain attributes about a user, and another class User_Review that has a list of the objects/instances created by class User. 我正在尝试创建一个类User,该类存储有关用户的某些属性,而另一个类User_Review具有由类User创建的对象/实例的列表。
class User(object):
user_objects = []
def __init__(self, name, age)
self.name = name
self.age = age
User.user_objects.append(self)
class User_Review(User):
user_list = # here is where I want user_objects to exist
user1 = User('leila', 23)
user2 = User('carter', 43)
# so user_list = [user1, user2]
To answer zondo and juanpa.arrivillaga, the purpose of User is to create objects with basic information, and User_Review is a method where I'd like to be able to do things with the list of users through other methods in the User_Review class, such as have methods that adds/removes users to the end of the user list, or display certain information (name only, for example). 为了回答zondo和juanpa.arrivillaga,User的目的是创建具有基本信息的对象,而User_Review是一种我希望能够通过User_Review类中的其他方法来处理用户列表的方法。以及将用户添加/删除到用户列表末尾或显示某些信息(例如仅名称)的方法。
For your purposes, I think you could instead of creating both the user_objects
and user_list
lists inside the classes, just retain the one inside User_Review
(since you want their references to be stored in another class), and get rid of the one inside User
, while simultaneously appending the created User
object to the user_list
from inside the __init__
method rather than user_objects
. 出于您的目的,我认为您可以代替在类内部创建
user_objects
和user_list
列表,而只需在User_Review
内部保留一个(因为您希望将它们的引用存储在另一个类中),并摆脱User
里面的一个,同时将创建的User
对象从__init__
方法内部而不是user_objects
附加到user_list
。
In practice, this would look something like the following. 在实践中,这看起来类似于以下内容。
class User(object):
def __init__(self, name, age):
self.name = name
self.age = age
User_Review.user_list.append(self)
class User_Review(object):
user_list = []
user1 = User('leila', 23)
user2 = User('carter', 43)
Printing out the name of the first user using print(User_Review.user_list[0].name)
correctly outputs carter
. 使用
print(User_Review.user_list[0].name)
打印出第一个用户的名称可以正确输出carter
。
This approach, however, seems fairly counter intuitive: 但是,这种方法似乎与直觉相反:
The User_Review
class inherits User
which to me does not make any logical sense, since you would not want to have a method such as change_age
in User_Review
if it happened to be implemented in User
. User_Review
类继承了User
,这对我来说没有任何逻辑上的意义,因为如果碰巧在User
实现了,则您不希望在User_Review
使用诸如change_age
的方法。 You would however, want to be able to access the data inside each User
object, which can be accomplished by just accessing the required User
inside the list. 但是,您希望能够访问每个
User
对象中的数据,这可以通过仅访问列表中所需的User
来实现。
As it stands, User_Review
serves no real purpose, exactly the same thing could be achieved by just a regular list; 就目前而言,
User_Review
没有实际目的,仅通过常规列表就可以实现完全相同的目的; however, if there are plans for implementing methods that rely on data from multiple or all User
objects created, then this might be a viable solution. 但是,如果计划实施依赖于多个或所有
User
对象创建的数据的方法,那么这可能是一个可行的解决方案。
Other solutions to this problem could be creating the list inside the User
class only, and just using User_Review
to access that list and do the required actions. 解决此问题的其他方法可能是仅在
User
类内部创建列表,而仅使用User_Review
访问该列表并执行所需的操作。 Or more complexly creating a separate class from these two dedicated to storing, organizing, and retrieving users to and from a database. 或者更复杂地从这两个类中创建一个单独的类,这些类专门用于在数据库中存储,组织和检索用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.