#๐ Using class before it is initialised
18 messages ยท Page 1 of 1 (latest)
@abstract badge
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.
Closes after a period of inactivity, or when you send !close.
Thats not possible
You must define a class before you can use it
Hmm so how should I do it?
class MyClass:
def __init__(self, a, b):
self.a = a
self.b = b
MYCONST = MyClass(1, 2)
like this else it won't work
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)
you can only call MYCONST if MYCONST is set
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
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.