Python : How to call a class methods(parent) in another class methods(child) only by importing class files(parent to child)?
I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.
so far i did below approach,
base class
import sqlite3
from sqlite3 import Error
class LocalDb:
def insert_method():
pass
have import insert_method to below class.
from access_db.local_db.local_db import LocalDb
class Main():
def __init__(self):
pass
def change(self):
LocalDb.iterate_db()
if __name__ == '__main__':
main = Main()
main.change()
when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"
also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.
here my questios are,
how to import class A methods in class B and how to call class A methods in class B?
if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?
or am i getting confuse with inheritance and importing class methods to one class? or both are same?
even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?
please some one help me on this.
database python-2.7 inheritance sqlite3
add a comment |
I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.
so far i did below approach,
base class
import sqlite3
from sqlite3 import Error
class LocalDb:
def insert_method():
pass
have import insert_method to below class.
from access_db.local_db.local_db import LocalDb
class Main():
def __init__(self):
pass
def change(self):
LocalDb.iterate_db()
if __name__ == '__main__':
main = Main()
main.change()
when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"
also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.
here my questios are,
how to import class A methods in class B and how to call class A methods in class B?
if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?
or am i getting confuse with inheritance and importing class methods to one class? or both are same?
even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?
please some one help me on this.
database python-2.7 inheritance sqlite3
add a comment |
I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.
so far i did below approach,
base class
import sqlite3
from sqlite3 import Error
class LocalDb:
def insert_method():
pass
have import insert_method to below class.
from access_db.local_db.local_db import LocalDb
class Main():
def __init__(self):
pass
def change(self):
LocalDb.iterate_db()
if __name__ == '__main__':
main = Main()
main.change()
when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"
also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.
here my questios are,
how to import class A methods in class B and how to call class A methods in class B?
if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?
or am i getting confuse with inheritance and importing class methods to one class? or both are same?
even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?
please some one help me on this.
database python-2.7 inheritance sqlite3
I'm new to Python, as of now trying to develop framework for a product. in class file A i have written db access functions like insert, delete, select and update. in main class file i have to import all above functions and have to use class A functions.
so far i did below approach,
base class
import sqlite3
from sqlite3 import Error
class LocalDb:
def insert_method():
pass
have import insert_method to below class.
from access_db.local_db.local_db import LocalDb
class Main():
def __init__(self):
pass
def change(self):
LocalDb.iterate_db()
if __name__ == '__main__':
main = Main()
main.change()
when i tired above thing im getting "TypeError: unbound method iterate_db() must be called with LocalDb instance as first argument (got classobj instance instead)"
also. I have tried, Main(LocalDb) for inheriting the methods of LocalDd but im hitting an error says that "sqlite3.OperationalError: no such table: albums" (but in db we have "albums" table in db), here im getting confused when i use insert_db method within the LocalDb class file im getting the expected results.
here my questios are,
how to import class A methods in class B and how to call class A methods in class B?
if answer for the 1 question is inheritance(multilevel) then is there any other solution for the question no 1?
or am i getting confuse with inheritance and importing class methods to one class? or both are same?
even inheritance also what i did in second approach, i couldn't hit the iterate_db method(it throws error.) how to call the parent method in child class?
please some one help me on this.
database python-2.7 inheritance sqlite3
database python-2.7 inheritance sqlite3
edited Nov 14 '18 at 6:37
Santhosh
asked Nov 14 '18 at 6:20
SanthoshSanthosh
146
146
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
although your method does not get self
as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.
what you need here is a static method (assuming you do not need an instance of the LocalDb
class available.
use
@staticmethod
decorator - for docomentation.
import the class withfrom filename import LocalDb
and its methods will come with it.irrelevent if we are using a static method.
- irrelevent if we are using a static method.
- for general knowledge here is the documentation of
super
- link, it is used to access the inherited class.
in summery the implementation of your code would look something like this:
class LocalDb:
@staticmethod
def insert_method():
pass
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294192%2fpython-how-to-call-a-class-methodsparent-in-another-class-methodschild-onl%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
although your method does not get self
as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.
what you need here is a static method (assuming you do not need an instance of the LocalDb
class available.
use
@staticmethod
decorator - for docomentation.
import the class withfrom filename import LocalDb
and its methods will come with it.irrelevent if we are using a static method.
- irrelevent if we are using a static method.
- for general knowledge here is the documentation of
super
- link, it is used to access the inherited class.
in summery the implementation of your code would look something like this:
class LocalDb:
@staticmethod
def insert_method():
pass
add a comment |
although your method does not get self
as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.
what you need here is a static method (assuming you do not need an instance of the LocalDb
class available.
use
@staticmethod
decorator - for docomentation.
import the class withfrom filename import LocalDb
and its methods will come with it.irrelevent if we are using a static method.
- irrelevent if we are using a static method.
- for general knowledge here is the documentation of
super
- link, it is used to access the inherited class.
in summery the implementation of your code would look something like this:
class LocalDb:
@staticmethod
def insert_method():
pass
add a comment |
although your method does not get self
as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.
what you need here is a static method (assuming you do not need an instance of the LocalDb
class available.
use
@staticmethod
decorator - for docomentation.
import the class withfrom filename import LocalDb
and its methods will come with it.irrelevent if we are using a static method.
- irrelevent if we are using a static method.
- for general knowledge here is the documentation of
super
- link, it is used to access the inherited class.
in summery the implementation of your code would look something like this:
class LocalDb:
@staticmethod
def insert_method():
pass
although your method does not get self
as its first parameter (represents the class instance) it needs to be defined as a static method to be used without an instance of the class.
what you need here is a static method (assuming you do not need an instance of the LocalDb
class available.
use
@staticmethod
decorator - for docomentation.
import the class withfrom filename import LocalDb
and its methods will come with it.irrelevent if we are using a static method.
- irrelevent if we are using a static method.
- for general knowledge here is the documentation of
super
- link, it is used to access the inherited class.
in summery the implementation of your code would look something like this:
class LocalDb:
@staticmethod
def insert_method():
pass
edited Nov 14 '18 at 11:02
answered Nov 14 '18 at 10:53
Omer Ben HaimOmer Ben Haim
1467
1467
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294192%2fpython-how-to-call-a-class-methodsparent-in-another-class-methodschild-onl%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown