classes

Discussion in 'Python' started by Pablo, Jul 19, 2003.

  1. Pablo

    Pablo Guest

    Hello everyone

    I'm rather new to Python and have a problem I hope someone can help me
    solve.
    I'm used to Java syntax and would like to make a class which holds a
    connection object to PostgreSQL database.
    I want to make this class as a singleton, and
    allow creation only of one instance of the class.

    code in Java would like more or less like this:

    class Connection{
    private static Connection instance = null;
    private DBConnection = null; // in python's case it would be pgobject returned
    be connect()

    private Connection(){
    DBConnection = pg.DB();
    }

    public static Connection getInstance(){
    if(instance == null)
    instance = new Connection();
    return instance;
    };

    How can I do the same thing in Python?
    How can I declare a static field (and private) of a class and allow access to it only by
    a static method?
    Can I forbid to instantiate more than one object?

    I'd be grateful if someone can explain it to me or send me to the good
    resource with description about classes which would have some comparisons
    of syntax regardig Java or C++.

    Best regards
    Pablo
    Pablo, Jul 19, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,924
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,192
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,391
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    893
    Andrew Thompson
    Jul 17, 2004
  5. Razvan
    Replies:
    5
    Views:
    11,272
    Dale King
    Jul 27, 2004
Loading...

Share This Page