SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 20th, 2004, 05:41 PM
jimo9 jimo9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 81 jimo9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 41 sec
Reputation Power: 6
Static Python

Hi. I am trying to create a class that will allow only one instance of itself to be created. I was looking for something like static(in c++) for python. Is there something like this? Thanks!

Reply With Quote
  #2  
Old April 20th, 2004, 06:30 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
Originally Posted by jimo9
Hi. I am trying to create a class that will allow only one instance of itself to be created. I was looking for something like static(in c++) for python. Is there something like this? Thanks!

What you want to do is to implement the Singleton pattern. You'll want to look at the Overriding the __new__ method section of Unifying types and classes. I've linked directly to the specific section where implementation is discussed but I recommend reading the entire essay.

Last edited by percivall : April 20th, 2004 at 06:35 PM.

Reply With Quote
  #3  
Old April 21st, 2004, 02:52 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,196 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 42 m 7 sec
Reputation Power: 252
There are several ways to create singletons in Python, and they are covered in depth in the Python Cookbook on ASPN.

http://aspn.activestate.com/ASPN/Co...on/Recipe/52558
http://aspn.activestate.com/ASPN/Co...on/Recipe/66531
http://aspn.activestate.com/ASPN/Co...on/Recipe/66531
http://aspn.activestate.com/ASPN/Co...n/Recipe/102187

Some of them use deep Python magic, such as metaclasses.

However the simplest way to create a singleton is to just use a module instead of a class. It is not as powerful as a class singleton since you can't use inheritance, but 99% of the time you don't need to.

I generally avoid singletons since they tend to increase coupling between classes and make unit testing harder.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Static Python


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway