Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 59 characters in body
Source Link
Sam Y
  • 116
  • 7

In Mark Lutz's book Learning Python, Fifth edition, he mentioned a way of simulating encapsulation of class level like this:

"""
Created on Sun Oct 4 10:16:30 2020
@author: Mark Lutz
A typical implementation of encapsulation in python,
to use, call:@private(‘var1’, ‘var2’...)
"""
def private(*values):
 def decorator(cls):
 class Proxy:
 def __init__(self, *args, **kwargs):
 self.inst = cls(*args, **kwargs)
 def __call__(self, cls, *args, **kwargs):
 return self.inst
 def __getattr__(self, attr):
 if attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: return getattr(self.inst, attr)
 def __setattr__(self, attr, val):
 # Allow access inside the class
 if attr == 'inst': self.__dict__[attr] = val
 elif attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: setattr(self.inst, attr, val)
 def __str__(self):
 return self.inst.__str__()
 return Proxy
 return decorator

this can be used for class-level encapsulation (e.g.limiting the access of a variable or method in a class).

For module-level encapsulation, however, the only way that I can think of is that you don't import that thing if you don't use it. Or else, create a Facade that limitfile and write the things yourinit.py. However if those who writes the client program knows the structure of your file / package, this can accessstill not stop them from importing stuff.

In Mark Lutz's book Learning Python, Fifth edition, he mentioned a way of simulating encapsulation of class level like this:

"""
Created on Sun Oct 4 10:16:30 2020
@author: Mark Lutz
A typical implementation of encapsulation in python,
to use, call:@private(‘var1’, ‘var2’...)
"""
def private(*values):
 def decorator(cls):
 class Proxy:
 def __init__(self, *args, **kwargs):
 self.inst = cls(*args, **kwargs)
 def __call__(self, cls, *args, **kwargs):
 return self.inst
 def __getattr__(self, attr):
 if attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: return getattr(self.inst, attr)
 def __setattr__(self, attr, val):
 # Allow access inside the class
 if attr == 'inst': self.__dict__[attr] = val
 elif attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: setattr(self.inst, attr, val)
 def __str__(self):
 return self.inst.__str__()
 return Proxy
 return decorator

this can be used for class-level encapsulation (e.g.limiting the access of a variable or method in a class).

For module-level encapsulation, however, the only way that I can think of is that you don't import that thing if you don't use it. Or else, create a Facade that limit the things your client program can access.

In Mark Lutz's book Learning Python, Fifth edition, he mentioned a way of simulating encapsulation of class level like this:

"""
Created on Sun Oct 4 10:16:30 2020
@author: Mark Lutz
A typical implementation of encapsulation in python,
to use, call:@private(‘var1’, ‘var2’...)
"""
def private(*values):
 def decorator(cls):
 class Proxy:
 def __init__(self, *args, **kwargs):
 self.inst = cls(*args, **kwargs)
 def __call__(self, cls, *args, **kwargs):
 return self.inst
 def __getattr__(self, attr):
 if attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: return getattr(self.inst, attr)
 def __setattr__(self, attr, val):
 # Allow access inside the class
 if attr == 'inst': self.__dict__[attr] = val
 elif attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: setattr(self.inst, attr, val)
 def __str__(self):
 return self.inst.__str__()
 return Proxy
 return decorator

this can be used for class-level encapsulation (e.g.limiting the access of a variable or method in a class).

For module-level encapsulation, however, the only way that I can think of is that you create a file and write the init.py. However if those who writes the client program knows the structure of your file / package, this can still not stop them from importing stuff.

Source Link
Sam Y
  • 116
  • 7

In Mark Lutz's book Learning Python, Fifth edition, he mentioned a way of simulating encapsulation of class level like this:

"""
Created on Sun Oct 4 10:16:30 2020
@author: Mark Lutz
A typical implementation of encapsulation in python,
to use, call:@private(‘var1’, ‘var2’...)
"""
def private(*values):
 def decorator(cls):
 class Proxy:
 def __init__(self, *args, **kwargs):
 self.inst = cls(*args, **kwargs)
 def __call__(self, cls, *args, **kwargs):
 return self.inst
 def __getattr__(self, attr):
 if attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: return getattr(self.inst, attr)
 def __setattr__(self, attr, val):
 # Allow access inside the class
 if attr == 'inst': self.__dict__[attr] = val
 elif attr in values:
 raise AttributeError("Private valueiables are not accessible!")
 else: setattr(self.inst, attr, val)
 def __str__(self):
 return self.inst.__str__()
 return Proxy
 return decorator

this can be used for class-level encapsulation (e.g.limiting the access of a variable or method in a class).

For module-level encapsulation, however, the only way that I can think of is that you don't import that thing if you don't use it. Or else, create a Facade that limit the things your client program can access.

lang-py

AltStyle によって変換されたページ (->オリジナル) /