#๐Ÿ”’ Using class before it is initialised

18 messages ยท Page 1 of 1 (latest)

abstract badge
#

How do I use a new class before it is initialised?
For example:

MYCONST = MyClass(1, 2)

class MyClass:
    def __init__(self, a, b):
        self.a = a
        self.b = b
prisma otterBOT
#

@abstract badge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

stray elk
#

You must define a class before you can use it

abstract badge
#

Hmm so how should I do it?

stray elk
#
class MyClass:
    def __init__(self, a, b):
        self.a = a
        self.b = b

MYCONST = MyClass(1, 2)
#

like this else it won't work

abstract badge
#

What if my method uses it?

#
class MyClass:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def my_method(self):
        return MYCONST

MYCONST = MyClass(1, 2)
stray elk
#

using MyClass

#
class MyClass:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def my_method(self):
        return MYCONST

MYCONST = MyClass(1, 2) # Set MYCONST
print(MYCONST.my_method()) # Return MYCONST via the function
#

if you put any if the functions above each it won't work since they are not defined

abstract badge
#

I see

#

Thx

#

!close

prisma otterBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.