classes

Pablo pablo at bogus.domain.org
Sat Jul 19 16:46:34 EDT 2003


Hello everyone

I have a problem with classes and hope someone can help me.
I'm used to Java and want to make a singleton class which would allow to
create only one instance of the class.
In Java it looks as follows:
public class DBConnection{
  private static DBConnection instance = null;
  private Conn conn = null;  // it is supposed to be object returned be
pg.connect() method of the PyGreSQL module

  /* a constructor must be private to forbid instantiation of the class */
  private DBConnection(){
    conn = new Conn();
  }

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

How can I do the same in Python?
My main problems are:
1) how to declare a static field (class field not object field)
2) how to declare a static method
3) how to declare a private constructor (is it possible?)

Can someone help me or point me to some documentation with strong emphasis
on classes? It would be perfect if that documentation had some comparisons
with classes in Java or C++.

Best regards
Pablo




More information about the Python-list mailing list