#🔒 call a father class when needed

76 messages · Page 1 of 1 (latest)

scenic sundial
#

here is my code it's simple i just wanted to learn all topics i recently learned


def run_in_thread(fun):
    def wrapper(*args, **kwargs):
        thread = threading.Thread(target=fun, args=args, kwargs=kwargs)
        thread.start()

    return wrapper

class Operations:
    def __init__(self, num1 : float, num2 : float) -> None:
        self.num1 = num1
        self.num2 = num2

    @run_in_thread
    def plus(self) -> float:
        return self.num1 + self.num2
    
    @run_in_thread
    def minus(self) -> float:
        return self.num1 - self.num2
    
    @run_in_thread
    def multiply(self) -> float:
        return self.num1 * self.num2
    
class Calculator(Operations):
    def __init__(self, color : str) -> None:
        self.color = color
        self.operaions = ["PLUS", "MINUS", "MULTIPLY"]
        print(f"calculator running in {self.color} color")

    def console(self):
        while True:
            print(self.operaions)
            opr = input("select a operation> ")
            if opr in self.operaions:
                if opr == "PLUS":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    super().__init__(num1, num2).plus()
                elif opr == "MINUS":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    super().__init__(num1, num2).minus()
                elif opr == "MULTIPLY":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    super().__init__(num1, num2).multiply()

            else:
                print("selected wrong operation!\nexiting...")
                exit()

Calculator('blue').console()```
edgy anchorBOT
#

@scenic sundial

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.

scenic sundial
#

here is my code and that's the error i'm getting and idk what to do because in this artcile:
https://www.tutorialspoint.com/how-to-call-a-parent-class-method-in-python#:~:text=In Python%2C you can call,you to access its methods.
i can run the method in child class in need using super but idk how to dix this
here the error:

  File "C:\Users\bildiran.co\Documents\python_test\simple_calculator1.py", line 41, in console
    super().__init__(num1, num2).plus()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'plus'
frigid zodiac
#

please can you add py to the beginning of your codeblock ?

edgy anchorBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

frigid zodiac
#

super().__init__() is only calling the constructor from your parent class into your child class, it doesn't return anything (as constructors dont return anything)

#

after doing super().__init__(num1, num2)
you can do self.plus() etc...

scenic sundial
#

but now it's returning None

frigid zodiac
#

wdym ?

scenic sundial
#
while True:
            print(self.operaions)
            opr = input("select a operation> ")
            if opr in self.operaions:
                if opr == "PLUS":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    super().__init__(num1, num2)
                    print(self.plus())
                elif opr == "MINUS":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    super().__init__(num1, num2)
                    print(self.minus())
                elif opr == "MULTIPLY":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    super().__init__(num1, num2)
                    print(self.multiply())
#

here is the fixed code

frigid zodiac
#

your prints return you none ?

scenic sundial
#

when i run code

scenic sundial
frigid zodiac
#

oh sry i didnt see you called super().__init__ in a method

#

you have to call it in the constructor

#

so you can remove all your super().__init__ and just call it once in Calculator.__init__

tender field
#

and probably add a method to change the values of num1 & num2 in the Operator class

frigid zodiac
#

also you're not receiving the return value from the thread

scenic sundial
#

but look at code it doesn't return operation before accessing it from user input

frigid zodiac
#

it does though, you can see in your console the list of operations

scenic sundial
#

but i just want to send one operation from the list

#

not all

frigid zodiac
#

well you print all operations, it's only then that you choose given what the user has inputted

scenic sundial
#

ok

#

ok

frigid zodiac
#

super().__init__ only launches the constructor from the parent

#

it wont do all operations

scenic sundial
#

asfter that

frigid zodiac
#

but you need to init your parent to be able to use its attributes defined in its constructor

scenic sundial
#

rn

frigid zodiac
#
class Calculator(Operations):
    def __init__(self, color : str) -> None:
        self.color = color
        self.operaions = ["PLUS", "MINUS", "MULTIPLY"]
        super().__init__(num1, num2)
        print(f"calculator running in {self.color} color")

    def console(self):
        while True:
            print(self.operaions)
            opr = input("select a operation> ")
            if opr in self.operaions:
                if opr == "PLUS":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    self.plus()
                elif opr == "MINUS":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    self.minus()
                elif opr == "MULTIPLY":
                    num1 = input("num1: ")
                    num2 = input("num2: ")
                    self.multiply()

            else:
                print("selected wrong operation!\nexiting...")
                exit()
#

this wont print anything though because you don't receive the output from the thread

scenic sundial
#

hmm

frigid zodiac
#

good way to do it would be to return the thread in your wrapper function, and then do .join() after your operation and print that

tender field
#

actually this would raise a NameError

#

num1 and num2 are not defined as of yet when __init__ is called

scenic sundial
frigid zodiac
scenic sundial
#

in line 5

tender field
frigid zodiac
#

ohw ait yes forgot something

scenic sundial
#

we don't have access to num1 and num2 but how could you parse them there?

frigid zodiac
#
class Calculator(Operations):
    def __init__(self, color : str) -> None:
        self.color = color
        self.operaions = ["PLUS", "MINUS", "MULTIPLY"]
        print(f"calculator running in {self.color} color")

    def console(self):
        while True:
            print(self.operaions)
            opr = input("select a operation> ")
            if opr in self.operaions:
                if opr == "PLUS":
                    self.num1 = input("num1: ")
                    self.num2 = input("num2: ")
                    self.plus()
                elif opr == "MINUS":
                    self.num1 = input("num1: ")
                    self.num2 = input("num2: ")
                    self.minus()
                elif opr == "MULTIPLY":
                    self.num1 = input("num1: ")
                    self.num2 = input("num2: ")
                    self.multiply()

            else:
                print("selected wrong operation!\nexiting...")
                exit()
#

this is better

scenic sundial
#

where is super

frigid zodiac
#

you don't need it if you define the attributes directly

#

also you'd have to convert the values

#

(but i'd do it within the operation methods)

scenic sundial
#

alr i'm so confused rn

frigid zodiac
#

well Calculator is a child of Operations meaning it inherits plus, minus and multiply from it

scenic sundial
frigid zodiac
#

but those functions need num1 and num2 as attributes

scenic sundial
#

yes

frigid zodiac
#

so here i simply define self.num1 and self.num2 before calling the operation methods

scenic sundial
#

oh

#

oh

#

i didn't see it before

#

that's right

#

@frigid zodiac so i can just put None deafult in Operations class and define num1 and 2 after

frigid zodiac
#

for instance, but what you did also allow to do stuff like Operation(1, 3).sum()

#

which is also cool i guess

scenic sundial
#

cool cool

#

ty

#

let me see if it works

#

@frigid zodiac it's like instaed of using super
we just define parms with self

frigid zodiac
#

(i have to go sorry, ill let you in the hands of someone else, please note you still have to fix the threading issue)

scenic sundial
#

right?

frigid zodiac
#

yes

scenic sundial
#

@frigid zodiac doesn't work again lmfao

edgy anchorBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.