Python Forum
Classes are blowing my mind!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes are blowing my mind!
#1
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__().
Reply
#2
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()
Reply
#3
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().
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
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)
Reply
#5
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.
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,824 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Python seems to be randomly blowing up parmort 6 3,242 Aug-22-2018, 09:47 PM
Last Post: parmort
  Using classes? Can I just use classes to structure code? muteboy 5 5,037 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020