Python Forum
Classes are blowing my mind! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Classes are blowing my mind! (/thread-1418.html)



Classes are blowing my mind! - mcmxl22 - Jan-01-2017

I have been trying to figure out classes and I can't wrap my brain around it. 
I believe I know the syntax for creating classes but when it comes to getting them to run I get lost quickly.
I get unbound method, not enough arguments, and instance errors.
here is my latest example.

import sqlite3


class create_database(object):

    def createdb(self):
        conn = sqlite3.connect('bid.db') 
        cursor = conn.cursor()
        sql = '''create table bid (
            colum1 text,
            colum2 int,
            colum3 int)'''
        cursor.execute(sql)
        cursor.close()


class main(object):
    def __init__(self):
        bid = create_database()
        bid

main()
This doesn't produce any errors but it doesn't create the bid.db file either.
As a standalone function creatdb() works fine.
I guess I don't know how to call a function from within a class using __init__().


RE: Classes are blowing my mind! - snippsat - Jan-01-2017

You need to call method createdb
class main(object):
    def __init__(self):
        bid = create_database()
        bid.createdb()

main()
If remove main() class,you would call it like like this.
bid = create_database()
bid.createdb()
Could also put __init__ in first class.
import sqlite3

class create_database(object):
    def __init__(self):
        conn = sqlite3.connect('bid.db')
        cursor = conn.cursor()
        sql = '''create table bid (
            colum1 text,
            colum2 int,
            colum3 int)'''
        cursor.execute(sql)
        cursor.close()
Then it will be created with create_database()


RE: Classes are blowing my mind! - Ofnuts - Jan-02-2017

Classes typically represent objects and not actions on these objects (the actions are typically the "methods" of the classes. When you name a class create_database something is likely wrong. Normally you would have a Database class, with methods such as open(), request() and close().


RE: Classes are blowing my mind! - Larz60+ - Jan-02-2017

If you are familiar with 'C', the class would equate to a structure.

In order to use it, you must create an instance of it.

Thus:
 bid = create_database()
creates an instance of create_database

You can create another (and distinct) instance like
bid1 = create_database()
To run a method in a particular instance, you would code instance_name.method_name
thus
bid.createdb()
(or
bid1.createdb()
for the second instance if present)


RE: Classes are blowing my mind! - mcmxl22 - Jan-02-2017

Thank you @Larz60+ and @snippsat for taking the time to explain it to me. I am new to the world of coding and Python is my first language.


RE: Classes are blowing my mind! - snippsat - Jan-03-2017

As mention bye Ofnuts naming class create_database with a method createdb is not a good option.
This show a way one how it could look.
import sqlite3

class Bidbase(object):
    def __init__(self):
        self.db = sqlite3.connect('bid.db')

    def create_db(self):
        cursor = self.db.cursor()
        sql = '''create table bid (
            id INTEGER PRIMARY KEY,
            name TEXT,
            phone TEXT)'''
        cursor.execute(sql)
        cursor.close()

    def insert(self, name, phone):
        cursor = self.db.cursor()
        cursor.execute('''INSERT INTO bid(name, phone)
                       VALUES(?, ?)''', (name, phone))
        self.db.commit()
        cursor.close()

    def read_all(self):
        cursor = self.db.cursor()
        cursor.execute('''SELECT name, phone FROM bid''')
        all_rows = cursor.fetchall()
        for row in all_rows:
             print('{0} : {1}'.format(row[0], row[1]))
        cursor.close()

bid = Bidbase()
bid.create_db()
bid.insert('super', '345678')
bid.insert('Tom Hope', '99999999')
bid.insert('Hans Gloom', '77889944')
bid.read_all()
Output:
super : 345678 Tom Hope : 99999999 Hans Gloom : 77889944
Now second run we can insert and read_all,
because __init__  self.db is shared with all method,
and taking out of create_db method.
>>> bid = Bidbase()

>>> bid.insert('Superman', '666666')
>>> bid.insert('Hulk', '987654321')

>>> bid.read_all()
super : 345678
Tom Hope : 99999999
Hans Gloom : 77889944
Superman : 666666
Hulk : 987654321