How do I return a reference to something inside a RefCell without breaking encapsulation?

You can create a new struct similar to the Ref<‘a,T> guard returned by RefCell::borrow(), in order to wrap this Ref and avoid having it going out of scope, like this: use std::cell::Ref; struct FooGuard<‘a> { guard: Ref<‘a, MutableInterior>, } then, you can implement the Deref trait for it, so that it can be used as … Read more

Why are Python’s ‘private’ methods not actually private?

The name scrambling is used to ensure that subclasses don’t accidentally override the private methods and attributes of their superclasses. It’s not designed to prevent deliberate access from outside. For example: >>> class Foo(object): … def __init__(self): … self.__baz = 42 … def foo(self): … print self.__baz … >>> class Bar(Foo): … def __init__(self): … … Read more

tech