#πŸ”’ OOP multiple class inheritance.

18 messages Β· Page 1 of 1 (latest)

sick jacinth
#

Think about 20 objects inside of different .py file. And you want to inherit all of it into ine class. What's best practices to do it?

stone starBOT
#

@sick jacinth

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

native roost
#

you probably, almost certainly shouldn't be doing that

#

if you describe your 20 classes then we can most likely come up with a better solution

sick jacinth
#

I think so too. But i didn't write project from the beginning. They have written automation process with modules of the website. And every object is one job. After they must be used for different roles. Which leads to massive inheritance. So i am kinda bamboozled

#

Any ideas?

radiant scarab
#

some form of adapter based pattern sounds appropriate for this imo

sonic hare
sick jacinth
#

After a research i saw rocketchat doing something

#

Let me find it again

#

from rocketchat_API.APISections.assets import RocketChatAssets
from rocketchat_API.APISections.banners import RocketChatBanners
from rocketchat_API.APISections.channels import RocketChatChannels
from rocketchat_API.APISections.chat import RocketChatChat
from rocketchat_API.APISections.groups import RocketChatGroups
from rocketchat_API.APISections.im import RocketChatIM
from rocketchat_API.APISections.integrations import RocketChatIntegrations
from rocketchat_API.APISections.invites import RocketChatInvites
from rocketchat_API.APISections.livechat import RocketChatLivechat
from rocketchat_API.APISections.miscellaneous import RocketChatMiscellaneous
from rocketchat_API.APISections.permissions import RocketChatPermissions
from rocketchat_API.APISections.roles import RocketChatRoles
from rocketchat_API.APISections.rooms import RocketChatRooms
from rocketchat_API.APISections.settings import RocketChatSettings
from rocketchat_API.APISections.statistics import RocketChatStatistics
from rocketchat_API.APISections.subscriptions import RocketChatSubscriptions
from rocketchat_API.APISections.teams import RocketChatTeams
from rocketchat_API.APISections.users import RocketChatUsers
from rocketchat_API.APISections.video_conferences import RocketChatVideConferences

class RocketChat(
RocketChatUsers,
RocketChatChat,
RocketChatChannels,
RocketChatGroups,
RocketChatIM,
RocketChatIntegrations,
RocketChatStatistics,
RocketChatMiscellaneous,
RocketChatSettings,
RocketChatRooms,
RocketChatSubscriptions,
RocketChatAssets,
RocketChatPermissions,
RocketChatInvites,
RocketChatVideConferences,
RocketChatLivechat,
RocketChatTeams,
RocketChatRoles,
RocketChatBanners,
):

#

I don't know this is the best practice so i wrote here maybe some senior can give an advice

sick jacinth
sonic hare
sick jacinth
#

Because one user(role) can do 20 jobs

tall swift
#
  • If the behavior of these classes does not rely on state, then why not just either convert it into functions or instantiate a new instance whenever it's needed.
  • If only one job is necessary for each instance of RocketChat, then you can just provide one job
  • If you're not providing any data in the objects, why not just instantiate the objects when they are needed. You could even possibly provide needed data into the RocketChat constructor (which is especially relevant if the data in all of those jobs are similar)
  • If neither of those options above works for you you can always store a dictionary with all of the jobs. Probably a good idea to use enumerations for that. Really not the best solution though
stone starBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.