Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pass instance of itself as an argument to another function

Tags:

python

I have a UserModel class that will essentially do everything like login and update things.

I'm trying to pass the instance of itself (the full class) as an argument to another function of another class.

For example: (obviously not the code, but you get the idea)

from Car import CarFactory

class UserModel:
    def __init__(self,username):
        self.username = username

    def settings(self,colour,age,height):
        return {'colour':colour,'age':age,'height':height}

    def updateCar(self,car_id):
        c = CarFactory(car_id, <<this UserModel instance>>)

So, as you can see from the very last line above I would like to pass an instance of UserModel to the CarData class, so when within the CarData class I can access the UserModel.settings(), however, I am unsure of the syntax. I could of course just do:

c = CarFactory(car_id,self.settings)

Any help would be grateful appreciated.

Thanks

like image 523
user1741694 Avatar asked Mar 19 '26 13:03

user1741694


1 Answers

c = CarFactory(car_id, self)

doesnt work?

on a side note it would be self.settings() not self.settings ... unless you define settings to be a property

like image 163
Joran Beasley Avatar answered Mar 21 '26 02:03

Joran Beasley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!