Posts

Showing posts with the label Private

Calling Private Function Within The Same Class Python

Answer : There is no implicit this-> in Python like you have in C/C++ etc. You have to call it on self . class Foo: def __bar(self, arg): #do something def baz(self, arg): self.__bar(arg) These methods are not really private though. When you start a method name with two underscores Python does some name mangling to make it "private" and that's all it does, it does not enforce anything like other languages do. If you define __bar on Foo , it is still accesible from outside of the object through Foo._Foo__bar . E.g., one can do this: f = Foo() f._Foo__bar('a') This explains the "odd" identifier in the error message you got as well. You can find it here in the docs. __bar is "private" (in the sense that its name has been mangled), but it's still a method of Foo , so you have to reference it via self and pass self to it. Just calling it with a bare __bar() won't work; you have to call i...

Change Private Static Final Field Using Java Reflection

Answer : Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the modifier to get rid of final , and actually modify a private static final field. Here's an example: import java.lang.reflect.*; public class EverythingIsTrue { static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } public static void main(String args[]) throws Exception { setFinalStatic(Boolean.class.getField("FALSE"), true); System.out.format("Everything is %s", false); // "Everything is true" } } Assuming no SecurityException is thrown, the above code prints "Everything is true...