简体   繁体   English

Python 2.7如何从存储在不同类中的列表访问由一个类创建的对象

[英]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_objectsuser_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.

相关问题 如何访问使用 Python 中不同类中的一个类创建的对象 - How to access objects created using one class from a different class in Python Python 2.7:字典中类对象的列表串联 - Python 2.7: list concatenation for class objects within a dictionary 如何访问和使用存储在另一个类的 init 中的类中的数据? (Python) - How do I access and use data from a class which is stored within the init of another class? (Python) Python 2.7:在另一个类中创建的类如何查询创建它的类的变量? - Python 2.7: How can a class created inside another one query a variable of the class creating it? Python-如何访问列表中某个类中上一个类的变量? - Python - How can I access variables of an above class within a class within a list? 如何将 object 从不同的 class 访问到 ZA7F5F35426B9274111377Z38 中的 class 中? - How can you access an object from a different class into one class in Python? python 2.7中的类对象的级联列表 - cascade list of class objects in python 2.7 如何从python中的类外部访问对象列表 - How to access a List of Objects from outside a class in python Python:如何为在另一个类的实例中创建的类的实例传递属性值? - Python: How do I pass a value for an attribute for an instance of a class that is being created within an instance of a different class? 如何在 Python 中从一个 class 到另一个 class 调用列表? - How can a list be called from one class to another class in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM