Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Last active September 8, 2018 08:31
Show Gist options
  • Save mitsuse/b2de5aa71452d8a62c974d11450a1308 to your computer and use it in GitHub Desktop.
Save mitsuse/b2de5aa71452d8a62c974d11450a1308 to your computer and use it in GitHub Desktop.
A workaround for private __init__ by using name mangling.
#!/usr/bin/env python3
from __future__ import annotations
class Foo:
class __Internal:
pass
def __init__(self, _: Type[__Internal]) -> None:
pass
@staticmethod
def empty() -> Foo:
return Foo(Foo.__Internal)
Foo.empty() # OK
Foo(Foo.__Internal) # NG: AttributeError: type object 'Foo' has no attribute '__Internal'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment