The problem of instantiating parameters of class

class A:
    def __init__(self,x)
        self.x = x
    
    def a(self, a):
        return print(a)
        
    def b(self, b):
        return print(b)

ask the above class. I want to use the a method. The instantiation must enter a x before I can type the a method.
is there any way not to pass in the x instantiation and then use the a method. Because sometimes the method does not have the value of x used by .
Thank you.

Mar.30,2021

in that case, it is possible

  • do not use the x parameter
class A:

    def __init__(self, x):
        self.x = x

    def a(self, a):
        return print(a)

    def b(self, b):
        return print(b)

A = A(object)
print(A.a(5))
  • use the x parameter

class A:

    def __init__(self, x):
        self.x = x

    def a(self, a):
        return print(a)

    def b(self):
        return print(self.x)

B = A(3)
print(B.b())

can be called directly using the class, but it is not recommended.
like the following way:
A.a ('whatever','a')
' whatever' is just a placeholder parameter and can be entered at will, as long as instead of the 'slef' parameter'
'a' is the parameter that the method needs to pass in


if you can't use the value of the object, @ staticmethod know about it.

Menu