A
A
Alexey Kisilev2020-05-24 15:42:31
Python
Alexey Kisilev, 2020-05-24 15:42:31

How to call a method inside a class?

Tell me how to do it right.

I need to call the get_connection method inside another get_address method.
After that, outside the class, I need to call the get_address method

class DataBase():
    def get_connection(self):
        global __connection
        if __connection is None:
            __connection = sqlite3.connect('univer_info.db')
        return __connection

    def get_address(keyword: str):
        conn = get_connection()
        c = conn.cursor()
        c.execute('SELECT nameAddress FROM address WHERE keyWord=?', (keyword,))
        (res,) = c.fetchone()
        return res


here's a challenge

print(DataBase.get_address('2'))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Karbivnichy, 2020-05-24
@Reshk_0

self.get_address('2')?
Calling a class method in another method of the same class

A
Alexey Kisilev, 2020-05-24
@Reshk_0

Already decided - just moved the method outside the class

Y
yav0, 2020-05-24
@yav0

class A(object):
    def aaa(self, a):
        print("A.a = ", a)

    def bbb(self, b):
        A.aaa(None, b)


a1 = A()
a1.bbb('ccc')
a1.aaa('ca')
A.bbb(None,'ddd')
A.aaa(None,'da')

This code explains the difference between calling class methods and instance methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question