I'm wondering if I can reproduce the java code below using python, it declares a static attribute Hello
and assigns a new object, when I call MyClass.Hello
twice it returns the same reference because it's static.
class MyClass {
public final String message;
public MyClass(String message) {
this.message = message;
}
public static MyClass Hello = new MyClass("hello world");
}
public class HelloWorld {
public static void main(String []args){
// output true
System.out.println(MyClass.Hello == MyClass.Hello);
}
}
I've tried the following code, unfortunately it returns different reference. I'm open to alternative solutions
class MyClass:
def __init__(self, message):
self.message = message
@classmethod
@property
def Hello(cls):
return cls(message="hello world")
# output False
print(MyClass.Hello == MyClass.Hello)