I have a list that store User
objects:
profiles_catalog = []
and the 'User' object:
cont = 1
class User:
def __init__(self, name, password):
global cont
self.registry = cont
self.name = name
self.password = password
self.liked = []
self.matches = []
cont = 1
def __str__(self) -> str:
return f'Registry: {self.registry}, Name: {self.name}, Password: {self.password}, Liked_list: {self.liked}, matches: {self.matches}'
def __dict__(self) -> dict:
return {'Registry': {self.registry}, 'Name': {self.name}, 'password': {self.password}, 'Liked list': {self.liked}, 'matches': {self.matches}}
@property
def call_name(self) -> None:
return self.name
@property
def call_password(self) -> None:
return self.password
@property
def call_liked_list(self) -> None:
return self.liked
@call_liked_list.setter
def model_like_profile(self, liked_user) -> None:
self.liked.append(liked_user)
return f'{liked_user.name} got liked'
def see_matches(self) -> None:
return self.matches
This is supposed to be a dating app, as tinder or something, that show random profiles to user and it should be able to like profile or keep seeing other profiles.
To do that, I write functions show_random_profile()
and main_like_profile()
:
def show_random_profile():
displayed_user = profiles_catalog[randint(0, len(profiles_catalog) - 1)]
return displayed_user
and
def main_like_profile(control_user, displayed_user):
liking_user = control_user
liked_user = displayed_user
liking_user.model_like_profile(liking_user, liked_user)
print('User liked!')
but I'm keeping getting TypeError:
Traceback (most recent call last):
File "c:\Users\Lucas\Documents\LUCAS\programacao\python\meus_projetos\twindder\controller.py", line 140, in <module>
main()
File "c:\Users\Lucas\Documents\LUCAS\programacao\python\meus_projetos\twindder\controller.py", line 109, in main
main_like_profile(control_user, displayed_user)
File "c:\Users\Lucas\Documents\LUCAS\programacao\python\meus_projetos\twindder\controller.py", line 34, in main_like_profile
liking_user.model_like_profile(liking_user, liked_user)
TypeError: 'list' object is not callable
The way I see, the problem is that a list
object it is been assign to a variable that should take User
, is that right?
If so, how can I assign to displayed_user
a random User
object stored in profiles_catalog
?
Or there is any other way to solve this problem?
CodePudding user response:
With your current code, you define the model_like_profile
method with the decorator @call_liked_list.setter
applied to it, where call_liked_list
is a property. This makes model_like_profile
also refer to the same call_liked_list
property. So, the line:
liking_user.model_like_profile(liking_user, liked_user)
... is equivalent to liking_user.call_liked_list(liking_user, liked_user)
, which is equivalent to liking_user.liked(liking_user, liked_user)
. Since liking_user.liked
is a list, this line of code attempts to call a list as if it were a function, hence the TypeError
.
Keeping the User
class as you've written it, to access the functionality defined in model_like_profile
, you could change from:
liking_user.model_like_profile(liking_user, liked_user)
to:
liking_user.model_like_profile = liked_user
Although this use of a property setter for adding to a list is generally not good practice - a property setter is usually used for assigning a new value, not adding to a list. Also, it is good practice for the property setter's method name to be the same as the getter's method name:
class MyClass:
def __init__(self):
self._value = 1
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value