How to restrict users from creating a python object?
I have a domain object 开发者_JS百科User
. I am looking for some way to restrict other python classes form creating any objects of `user' type.
You can use closures to hide the object away from the code but please accept that doing it in Python is considered very unpythonic and is pretty much against the philosophy of the language. You are supposed to document proper behavior, not guard against your users.
class KittenFactory(object):
def __create_teh_secret_objectz(self):
class Kitten(object):
i_can_haz_invisible = True
return Kitten()
Willing programmers will still be able to either copy()
it or get its type()
and instantiate it manually.
In other words: don't go there.
精彩评论