Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中与多个类建立关系?或者如何在另一个类中创建类对象?_Python_Class - Fatal编程技术网

如何在Python中与多个类建立关系?或者如何在另一个类中创建类对象?

如何在Python中与多个类建立关系?或者如何在另一个类中创建类对象?,python,class,Python,Class,我已经用python为后端工作创建了一个模型 这是我的python代码 class User: def __init__(self, _id=None): self._id = _id if self._id is not None: self.contact = Contact() self.contact.user_id = self._id class Contact: def __in

我已经用python为后端工作创建了一个模型

这是我的python代码

class User:

    def __init__(self, _id=None):
        self._id = _id
        if self._id is not None:
            self.contact = Contact()
            self.contact.user_id = self._id


class Contact:

    def __init__(self, _id=None):
        self._id = _id
        self.user_id = None
        if self._id is not None:
            self.vehicle = Vehicle()
            self.vehicle.contact_id = self._id


class Vehicle:

    def __init__(self, _id=None):
        self._id = _id
        self.contact_id = None
        self.user_id = None

    def create_company(self, company_name):
        # check if user id is set
        if None in (self.contact_id, self.user_id):
            return {'result': False, 'msg': 'user or contact id is not set'}
        # here i will use user id and contact id
        return {'result': True, 'msg' : 'ok'}
我想创建一个链接到用户id和联系人id的车辆公司

我想这样做

User('user_id').Contact('contact_id').vehicle.create_company('any name')
但是我得到了错误,我知道为什么,但不知道如何解决

我收到的错误

AttributeError: 'User' object has no attribute 'Contact'

不知何故,我设法实现了这个代码,请建议更多更好的方法来做到这一点

class User:

    def __init__(self, _id=None):
        self._id = _id

    def Contact(self, _id):
        if self._id is None:
            raise Exception({'result':False, 'msg': 'user id is not set'})
        self.contact = Contact(_id)
        self.contact.user_id = self._id


class Contact:

    def __init__(self, _id=None):
        self._id = _id


    def Vehicle(self):
        if self._id is None:
            raise Exception({'result': False, 'msg': 'contact id is not set'})
        self.vehicle = Vehicle()
        self.vehicle.contact_id = self._id
        if hasattr(self, 'user_id'):
            self.vehicle.user_id = self.user_id



class Vehicle:

    def __init__(self, _id=None):
        self._id = _id

    def create_company(self, company_name=None):
        # check if user id is set
        if not hasattr(self, 'user_id') or not hasattr(self, 'contact_id'):
            return {'result': False}
        # here i will use user id and contact id
        return {'result': True, 'msg' : 'ok'}
像这样使用它

test = User('admin')
test.Contact('person')
test.contact.Vehicle()
print(test.contact.vehicle.create_company())
结果就是我想要的

{'result': True, 'msg': 'ok'}

你不能使用像那样的方法的另一个类。你的意思是
User('User_id')。contact.vehicle…
?如果可以的话,是的,但那样我无法定义contact id。然后你必须传递
contact
实例,或者将联系人id设置为
User
。我想在用户类中创建另一个函数,以获取具有联系人id的联系人类对象。但不知道这是否是正确的方法。