Re: Need Help with Encapsulation in Python!

2022-06-17 Thread Soham Mukherjee via Digitalmars-d-learn
[Here](https://www.scaler.com/topics/python/encapsulation-in-python/), they mentioned a way of simulating encapsulation of class level like this: ``` def private(*values): def decorator(cls): class Proxy: def __init__(self, *args, **kwargs): self.inst = cls

Re: Need Help with Encapsulation in Python!

2022-06-17 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 17 June 2022 at 14:14:57 UTC, Ola Fosheim Grøstad wrote: On Friday, 17 June 2022 at 13:58:15 UTC, Soham Mukherjee wrote: Is there any better way to achieve encapsulation in Python? Please rectify my code if possible. One convention is to use "self._fieldname" for protected and "sel

Re: Need Help with Encapsulation in Python!

2022-06-17 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Friday, 17 June 2022 at 13:58:15 UTC, Soham Mukherjee wrote: Is there any better way to achieve encapsulation in Python? Please rectify my code if possible. One convention is to use "self._fieldname" for protected and "self.__fieldname" for private class attributes.

Need Help with Encapsulation in Python!

2022-06-17 Thread Soham Mukherjee via Digitalmars-d-learn
``` self.a = 1 self.b = 2 self.c = 3 pass def __getattribute__(self, name): if sys._getframe(1).f_code.co_argcount == 0: if name in self.privates: raise Exception("Access to private attribute \"%s\" is not allowed" % name) else: return object.__g