0

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)
Raftel
  • 161
  • 7
  • I'm on python 3.8 and I obtained True from your code. – Kota Mori Aug 09 '21 at 02:17
  • @KotaMori I'm on python 3.9, [it adds the possibility](https://docs.python.org/3.9/library/functions.html#classmethod) to have both `@classmethod` and `@proprety` decorators. On python 3.9 MyClass.Hello is an instance of MyClass – Raftel Aug 09 '21 at 02:35
  • @TomKarzes I meant `MyClass.Hello`, I need it to be the same object. I indeed can achieve by setting the attribute after each class definition but the code will be difficult to read/maintain – Raftel Aug 09 '21 at 02:39
  • With how you have defined `MyClass.Hello` returns a new instance of `MyClass`. Python is NOT Java, trying to bring Java idioms into Python will in fact make it difficult to read/maintain. – metatoaster Aug 09 '21 at 02:41
  • @metatoaster I'm new to python, I would love to see how this can be done, I don't mind changing the code – Raftel Aug 09 '21 at 02:42
  • If you refer to the linked [answer here](https://stackoverflow.com/a/27481965/), simply defining a `Hello = MyClass(message="hello world")` attribute inside the `MyClass` definition will achieve what you want. – metatoaster Aug 09 '21 at 02:44
  • @metatoaster this raises `NameError: name 'MyClass' is not defined`, I can set a different class but not the same class – Raftel Aug 09 '21 at 02:48
  • 1
    Correction, you may need to assign it after the fact, since the class itself has to be fully defined and assigned to the global scope first, just set `MyClass.Hello = MyClass(...)` after the class definition. – metatoaster Aug 09 '21 at 02:49
  • oh thanks I didn't know you can extend the class after the declaration (not sure if it's considered bad practice), you may add the answer and I will accept it – Raftel Aug 09 '21 at 02:57
  • 1
    As I noted, Python is very much different to Java, there is no true private variables or final static variables - practically everything can be manipulated at will. Thus there is no point trying to enforce private or write-once only variables in Python, given that any implementation that enforce/emulate this can simply be undone by reversing the process. – metatoaster Aug 09 '21 at 03:00

0 Answers0