Reputation: 97
I've searched a lot about how to reuse a method from a class in the main.py file. i got some similar and basic solutions but in my case is a bit different.
/lib/module.py
class Myclass:
def __init__(self, x):
self.thisX = x
def check(self):
if self.thisX == 2:
print("this is fine. going to print it")
self.printing()
# this method will use in this class and must use from the main.py
# the parameter "z" is gonna use only when the method will call from main.py
def printing(self, z):
if z == 1 :
print("we got ", z)
else:
print(self.x)
/main.py
from lib.module import Myclass
# this is how i use the check() method once in my main.py
Myclass(2).check()
# the Myclass() gets "2" only once at the beginning of the program...
# i don't wanna pass "2" to the Myclass() everytime that i wanna use the printing() method...
c = Myclass()
c.printing(1)
error
TypeError: __init__() missing 1 required positional argument: 'x'
testing:
if i don't use the def init(), everything will be fine. but the problem is i need to keep it
Upvotes: 1
Views: 66
Reputation: 302
I think @richflow 's answer hit the point. If some variable is to be shared by all instances of a class, it's logical to assign its value using Myclass.x = new_number
. Then all instances of this class will know the change.
If you really want to optionally change x in the __init__
method of an instance, you can still do it. Combining with @richflow's codes, it can look like the following.
class Myclass:
x = 0
def __init__(self, x=None):
if x is not None:
Myclass.x = x
# other codes for initializiing the instance
def check(self):
if Myclass.x == 2:
print("this is fine. going to print it")
def printing(self, z=0):
if z == 1 :
print("we got ", z)
else:
print(Myclass.x)
I tried not to change too much from your codes. Your main.py
should work correctly with this class definition. However, the design looks a bit weird to me. Probably that's because I didn't understand clearly what the check
and printing
methods are really doing, and what the argument z
is really doing in printing
methods. If you provides more insights, probably people can help you with a better design.
Upvotes: 0
Reputation: 2144
This line in main.py:
c = Myclass()
Calls this function:
class Myclass:
def __init__(self, x):
self.thisX = x
Every time you create an instance of Myclass it will call the __init__()
function. You declared it to take 2 arguments: self
and x
. self
is always passed implicitly because it's a class, but you need to give it an argument 'x'.
So you can change main.py to this for example:
c = Myclass(2) # here x = 2
c.printing(1)
Please read this for more information
Also, in general, class names are written in CapWords style so it's a good idea to call your class MyClass
instead of Myclass
Edit:
Since you don't want to pass x
to __init__()
and you want to set x
from main.py you can try something like this:
class Myclass:
x = 0
def check(self):
if self.x == 2:
print("x is 2")
from main.py you can do:
Myclass.x = 2; #this only needs to be done once
Myclass().check()
Output:
x is 2
Upvotes: 1