Top/article/python-multimode-singleton
English | Japanese
English | Japanese

Menu

  • Top
  • Akihiko Yamaguchi 山口 明彦
  • Project プロジェクト
  • Text テキスト
  • Recent articles 最近の記事
  • Articles 記事一覧 (a to z)
  • Search 検索

Tags タグ †

  • [c++][bash][python][latex][php]
  • [linux][windows][mac][android]
  • [math][algorithm][idea][trick]
  • [liboctave][opencv][git][ros]
  • [setting][bug][general]
↑

Recent articles 最近の記事 †

2019-07-02
  • article/Display-Unix-Time
  • article/Synchronize-Linux-Time-to-Remote
2018-09-27
  • article/python-multimode-singleton
2018-09-02
  • article/rosinstall-git-default-remote
2017-07-28
  • article/SubMenu
2017-03-05
  • article/Import-a-different-version-of-OpenCV-in-Python
2015-08-17
  • article/DRC-finals-2015
2015-01-05
  • article/Upgrade-Android-to-Lollipop
2015-01-01
  • article/Kernel-panic-of-Linux-when-using-Xtion
  • article/Do-not-skip-freeing-data-when-using-tri-mesh-in-ODE
Access: 1/1554 - Admin

Implementing "Multimode" Singleton on Python

Pythonで"マルチモード"シングルトンを実装

[python]
2018-09-27

Multimode singleton means: Usually a singleton class has a single instance. A multimode singleton class has a mode parameter x; there is an instance for each different value of x. If x is in {1,3,9}, there are thee instances.

マルチモードシングルトンとは: 通常,シングルトンクラスはひとつのインスタンスを持つ.マルチモードシングルトンクラスは,モードパラメータ x を持ち,各 x の値につき,ひとつのインスタンスが作られる.例えば x が {1,3,9} のモードを持つなら,3つのインスタンスが存在しうる.

When is that useful? e.g. A velocity controller class of dual arm robots (x: arm index).

Implementation †

TMultiSingleton

↑

Usage †

Define a class where this class is used as the meta class.

class X(object):
  __metaclass__= TMultiSingleton

In X.__init__, the first argument should be the mode value (x).

In a function of X, use X._delete(self.x) to delete the instance (or reduce the reference count).

X can be instantiated like a usual class:

x1,x2,x3= X(1),X(1),X(2)

where x1 and x2 are the same instance (ids are the same) since they have the same mode value (1). x3 is a different instance.

↑

Reference †

  • https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html
  • https://kiwamiden.com/singleton-in-python
  • http://www.denzow.me/entry/2018/01/28/171416





Last-modified:2018-09-27 (Thu) 21:56:01 (1637d)
Site admin: Akihiko Yamaguchi.
Written by: Akihiko Yamaguchi.
System: PukiWiki 1.5.0. PHP 5.2.17. HTML conversion time: 0.015 sec.