classes

Pablo pablo at bogus.domain.org
Sat Jul 19 16:37:10 EDT 2003


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




More information about the Python-list mailing list