Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78111
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Class Inheritance from different module |
| Date | 2014-09-20 17:48 +0200 |
| Organization | None |
| References | <CAAp0bGv68m_6RtxOEPz4NFHOrbTyJa6tbP=qnR7267ZTEJ=yoQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14173.1411228159.18130.python-list@python.org> (permalink) |
Juan Christian wrote:
> I have the following structure:
>
> Third-party API installed via pip:
> steamapi /
> app.py
> consts.py
> core.py
> users.py
> [...]
>
> My script:
> test.py
>
>
> In the API, the module users.py has a class 'SteamUser' and I want to
> mimic it's usage on my code, like this:
>
> import steamapi
>
> [...]
>
> class User(Inheritance from API):
> def __init__(self, ID):
> steamapi.core.APIConnection(api_key = KEY)
> super( " Inheritance SteamUser" (ID)) # creates the user using the API
>
> [...]
>
> So that in my code when I need to create a new user, I just call 'usr =
> User("XXXXXXX")' instead of calling 'usr =
> steamapi.user.SteamUser(76561197996416028)', is that possible?
Yes, it doesn't matter in what module a baseclass is defined. Just go ahead
and import it:
# untested
import steamapi.user
class User(steamapi.user.User):
def __init__(self, userid=None, userurl=None):
super().__init__(userid, userurl)
# code specific to you subclass
> And of
> course, I want to have access to all the class methods like 'name',
> 'country_code', 'time_created', 'avatar', 'friends' and so on.
That's the very idea of subclassing.
> And finally, is that a good approach, pythonic?
It depends ;) If you are planning only small adjustments it might be an
option to write a few helper functions together with the original
steamapi.user.User class and be done.
> If not, what would be a good way?
Generally speaking the Python community often favours duck-typing or
composition over inheritance. I'm not prepared to go into a lengthy
discussion over this -- if you are interested you have to try a search
engine or rely on the usual suspects ;)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Class Inheritance from different module Peter Otten <__peter__@web.de> - 2014-09-20 17:48 +0200
csiph-web