Skip to content

Instantly share code, notes, and snippets.

>>> dir(waztile)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split',
'_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha',
'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
>>> waztile = "not poison"
>>> type(waztile)
<type 'str'>
>>> print waztile
not poison
>>> print waztile.istitle.__doc__
S.istitle() -> bool
Return True if S is a titlecased string and there is at least one
character in S, i.e. uppercase characters may only follow uncased
characters and lowercase characters only cased ones. Return False
otherwise.
def singleton(cls):
instances = {} # Line 2
def getinstance():
if cls not in instances:
instances[cls] = cls() # Line 5
return instances[cls]
return getinstance
class Counter:
def __init__(self):
>>> print Counter() is Counter()
True
class OneOnly {
private static final OneOnly the_one = new OneOnly();
private OneOnly() {
}
public static get() {
return the_one;
}
@singleton
class Counter:
def __init__(self):
self.count = 0
def inc(self):
self.count += 1
print type(Counter) # <type 'function'>
class Clown(object):
def __init__(self, name):
self.name = name
def nice_hi(self):
return "Helloooo " + self.__format_name()
def __format_name(self): # mangled to _Clown__format_name()
return self.name + " (the clown)"
class Clown(object):
def __init__(self, name):
self.name = name
def nice_hi(self):
return "Helloooo " + self.format_name()
def format_name(self):
return self.name + " (the clown)"
class Singleton(object):
def __new__(cls, *p, **k):
if not '_the_instance' in cls.__dict__:
cls._the_instance = object.__new__(cls)
return cls._the_instance
class Test(Singleton):
def __init__(self):
pass