-2

I have a domain object User. I am looking for some way to restrict other python classes form creating any objects of `user' type.

Pratik
  • 30,639
  • 18
  • 84
  • 159
Vijay Shanker Dubey
  • 4,308
  • 6
  • 32
  • 49

1 Answers1

1

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.

patrys
  • 2,729
  • 17
  • 27