I have four python scripts with below code:
main.py
from new import Name1
from new2 import Name2
class Details:
def __init__(self, id=0, vuse=True):
assert(id > 0)
self.id = id
self.vuse = vuse
def Main(self):
try:
self.dir = "/value/"
ti = Name1(id = self.id, dir = self.dir)
name = ti.getDay()
print name
except Exception, e:
print "Failed with %s" % e
if __name__ == "__main__":
at = Details(id = '1', vuse=True)
at.Main()
new.py
class Name1:
def __init__(self,id,dir=''):
self.id = id
self.dir = dir
def getDay(self):
try:
if id == 1:
self.day = "monday"
print "Name %s" % self.day
return self.day
else:
self.day = "Another"
print "Day %s" % self.day
return self.day
except Exception, e:
print "ERROR : %s" % e
new2.py
from find import Find
from main import Details
class Name2:
def getitems(self):
self.id = "// This value i want from main.py or new.py script"
at = Find(id = self.id )
val = at.value()
print val
if __name__ == "__main__":
a = Name2()
a.getitems()
find.py
class Find:
def __init__(self, id):
self.id = id
def value(self):
if self.id == 1:
print "Right value"
##// Do some operation with id//
else:
print "invalid"
My problem is how can i access the id value in new2.py…
if I will take the value from main.py, for calling the class i need to pass the id value which new2.py does not have. The same thing for new.py also, i need id to create an object of a class.
It’s not clear how your program is supposed to work, but you can initialize id in an instance of Name2 when you create it:
or pass it to getitems as an argument: