Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'third-party': 0.04; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'creates': 0.14; 'adjustments': 0.16; 'api,': 0.16; 'defined.': 0.16; 'inheritance': 0.16; 'key)': 0.16; 'possible?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subclass': 0.16; 'suspects': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'module': 0.19; 'not,': 0.20; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'finally,': 0.24; 'helper': 0.24; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'installed': 0.27; 'idea': 0.28; 'generally': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'way?': 0.31; 'class': 0.32; 'option': 0.32; 'community': 0.33; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'skip:s 30': 0.35; 'skip:u 20': 0.35; 'done.': 0.35; 'usual': 0.35; 'christian': 0.38; 'ahead': 0.38; 'depends': 0.38; 'to:addr:python-list': 0.38; 'planning': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'matter': 0.61; 'user,': 0.69; 'subject:Class': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Class Inheritance from different module Date: Sat, 20 Sep 2014 17:48:57 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdabd5.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411228159 news.xs4all.nl 2908 [2001:888:2000:d::a6]:57240 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78111 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 ;)