classes

P

Pablo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top