0

I'm having the following basic problem in Python. I want to create a constant static instance of a class within the class itself, to be used in methods of the class.

class MyClass(object):
    def __init__(self, i : int):
        self.i_c = i

    newinstance = MyClass(0)

    def method(self):
        if self == newinstance:
            return 'blaba'
        else:
            return self.i_c

Of course in this example, i could define the instance separately in the module containing my class, but then I could not use it in the methods of the class.

I don't know if it's feasible and I just don't know the right syntax, or if I cannot do this.

Edit: Based on the comments below one workaround is the following

class MyClass(object):
    def __init__(self, i : int):
        self.i_c = i

MyClass.newinstance = MyClass(0)

def func(thing):
    if thing == MyClass.newinstance:
        return 'blaba'
    else:
        return thing.i_c

This works, but func is now not a method of the class MyClass. This is not really problematic in my case, but I guess I can probably define func as a method of MyClass, I just don't really know how.

Ok, I got it, finally thanks to the comments below. This works

class MyClass(object):
    def __init__(self, i : int):
        self.i_c = i

    def method(self):
        if self == MyClass.newinstance:
            return 'blaba'
        else:
            return self.i_c

MyClass.newinstance = MyClass(0)
Ahr
  • 101
  • 1
  • 2
    "i could define the instance separately in the module containing my class, but then I could not use it in the methods of the class" - what? Why would defining the instance separately interfere with anything? – user2357112 Dec 31 '20 at 11:19
  • 1
    You cannot use the class object inside the class definition statement, the *class object doesn't exist yet*. Just assign it to the class after the class definition. But as mentioned already, there is noting stopping you from using an instance defined outside the class in the module inside the methods anyway... – juanpa.arrivillaga Dec 31 '20 at 11:20
  • Well in that case i could create newinstance after the definition of the class (on the same indentation level as MyClass()), but I could not use newinstance in the definition of "method" within the class. – Ahr Dec 31 '20 at 11:22
  • 1
    @Ahr what? You seem to have some fundamental misunderstanding, what you are saying isn't true at all. If after the class definition, you do `MyClass.newinstance = MyClass(0)` there is *nothing* stopping you from using that attribute in a method. Also, **again** there is nothing stopping you from just using a global variable. In either case, you could very quickly confirm that both of those options *would work* – juanpa.arrivillaga Dec 31 '20 at 11:23
  • Oh, ok, i will try that then. – Ahr Dec 31 '20 at 11:26
  • I'm sorry but i still dont get it. I can create an instance of MyClass as you say, yes, and define it as an attribute of the class. But I fail to understand how to use it inside the methods of MyClass. Sorry, if I'm being stupid, but in the code above, how would you go about using newinstance in the definition of method(self) ? – Ahr Dec 31 '20 at 11:40
  • Are you under the impression that a variable has to be defined before you can write methods that use it? That's not true. – user2357112 Dec 31 '20 at 11:51
  • Ah. Yes, I was under that impression very much ^^. Let me check then! – Ahr Dec 31 '20 at 11:56
  • Thanks!! This works!! I hadn't realize that i needed to declare newinstance in the class as MyClass.newinstance. Thank you!! – Ahr Dec 31 '20 at 12:02

0 Answers0