Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78107 > unrolled thread

Class Inheritance from different module

Started byJuan Christian <juan0christian@gmail.com>
First post2014-09-20 10:44 -0300
Last post2014-09-21 00:31 +1000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Class Inheritance from different module Juan Christian <juan0christian@gmail.com> - 2014-09-20 10:44 -0300
    Re: Class Inheritance from different module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-21 00:31 +1000

#78107 — Class Inheritance from different module

FromJuan Christian <juan0christian@gmail.com>
Date2014-09-20 10:44 -0300
SubjectClass Inheritance from different module
Message-ID<mailman.14170.1411220707.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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? And of
course, I want to have access to all the class methods like 'name',
'country_code', 'time_created', 'avatar', 'friends' and so on. And finally,
is that a good approach, pythonic? If not, what would be a good way?

[toc] | [next] | [standalone]


#78108

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-09-21 00:31 +1000
Message-ID<541d8fb5$0$29971$c3e8da3$5496439d@news.astraweb.com>
In reply to#78107
Juan Christian wrote:

[...]
> 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):

What do you mean, "Inheritance from API"? You can't inherit from an API,
only from classes.


> 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?

I would do something like this:

# untested
def make_user(ID):
    steamapi.core.APIConnection(api_key = KEY)
    return steamapi.user.SteamUser(76561197996416028)

usr = make_user("XXXXXXXXX")


No need for inheritance. make_user returns an actual SteamUser instance, not
some subclass.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web