Do I have to create and use objects when I use Python classes?

Asked 2 years ago, Updated 2 years ago, 21 views

When you use class methods, you can use all the classes by specifying objects in the class Do I have to use it like that? There's a reason, but I think it's just a class method It's easy to see that it's stuck together. I'm curious.

class main:
    def __init__(self,a,b):
        self.a = a
        self.b = b

    def one(self,c):
        self.c = c
        print(self.a, self.b, self.c)
        return self.a + self.b + self.c

    def two(self):
        print(self.a, self.b)b)
        return self.a + self.b

add = main(1,2)
print(add.one(3))
print(add.two())
print("---------------")
print(main(1,2).one(3))
print(main(3,4).two())

python

2022-09-22 18:32

1 Answers

I say this every time I reply, but if you search, the data is overflowing.

Even if you search "python static method" with a simple keyword, it comes out one after another. Try searching it.

https://stackoverflow.com/questions/735975/static-methods-in-python


2022-09-22 18:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.