I want the results of the method_test() to to be stored in variable a. So that I access it outside my class
class test:
def __init__(self):
a = method()
def method_test():
return "working"
check = test
print(check.a)
You need to set it as an attribute, which can be done with self.a:
class test:
def __init__(self):
self.a = test.method_test()
def method_test():
return "working"
check = test()
print(check.a)
#working
class test:
def __init__(self):
self.a = self.method_test()
def method_test(self):
return "working"
check = test()
print (check.a)
Related
I am new in Python, please ignore my explanation mentioned below, Basically I want a middle class(setter/getter) so that I can set values in one class and get values from any other class. I tried to make small program just for the solution purpose.
I am setting variable in class A
import sg
import testB
class A():
def __init__(self):
self.s = sg.SG()
def run(self):
self.s.setTest('123')
testB.B().run()
A().run()
using set get method class:
class SG():
test = ''
def __init__(self):
self.test = ''
print('I am in init')
def setTest(self, test1):
print('I am in set')
self.test = test1
def getTest(self):
print('I am in get')
return self.test
trying to retrieve variable value in class B:
import sg
class B():
def __init__(self):
pass
# self.s = sg.SG()
def run(self):
print("i am in B run")
sg.SG.getTest()
I have this code:
class Test:
def __init__(self, a):
self.a = a
self.success = True
def method(self):
self.success = False
test = Test("tree")
test.method
print(test.success)
#output is: True
I need to check if the operation programmed in "method" is successful. So, on success, I update the "success" attribute declared in the constructor. But when I call the method after creating the class object, the attribute is not updated.
To invoke a method,you have to use parenthesis. In short,
test.method() is the correct way to call the method.
You're not calling your method() correctly.
class Test:
def __init__(self, a):
self.a = a
self.success = True
def method(self):
self.success = False
test = Test("tree")
test.method() # NOTE the parentheses here
print(test.success)
I am making a class that i want to declare a variable which hold a function in it and i want to call them after i do some processing on some information.but i don't know how to use property decorator in this situation.
i already have this code:
class MyClass:
def __init__(self):
self.callback = None
def run():
#do something here...
result = self.callback(result)
print(result)
def func1(result):
result = result ** 2
return result
def func2(result):
result = result ** 4
return result
class1 = MyClass()
class1.callback = func1
class1.run()
class1.callback = func2
class1.run()
my question is how i can use #property and #property.setter and #property.getter decorator for self.callback in this code?
I based on this code don't see a need for properties but here it is anyway.
class MyClass:
def __init__(self):
self.__callback = None
#property
def cb(self):
return self.__callback
#cb.setter
def cb(self, new_cb):
self.__callback = new_cb
I want local variable of a class to be changed by other class. My script is similar to the following :
import datetime
b = []
class P:
def __init__(self):
self.count = 1
self.create()
def create(self):
global b
a = C(self.count)
for i in range(10):
a.print_name()
print b
class C:
def __init__(self, *data):
self.test = data[0]
#staticmethod
def print_name():
global b
b.append(datetime.datetime.now())
o = P()
How to avoid use of a global variable . On web i found use of "super " can resolve the issue . Please help in this regard .
Make C constructor to accept P instance. And call the method of P to append item to instance attribute of P object.
class P:
def __init__(self):
self.count = 1
self.items = []
self.create()
def append(self, item):
self.items.append(item)
def create(self):
a = C(self, self.count) # <-- Pass P instance (self)
for i in range(10):
a.print_name()
print self.items
class C:
def __init__(self, p_instance, *data):
self.p_instance = p_instance # <-- Save P instance to use later
self.test = data[0]
def print_name(self):
self.p_instance.append(datetime.datetime.now()) # <-- Call p instance method
You are probably looking for a class attribute. If you add b as an attribute to the C class, it can be accessed as C.b and C().b, i.e. from a reference to the class or any instance.
class C(object): # classes in py2 should inherit from object!
b = [] # b inside C definition
#classmethod
def print_name(cls):
cls.b.append(datetime.datetime.now())
class P(object):
def __init__(self):
self.count = 1
self.create()
def create(self):
a = C(self.count)
for i in range(10):
a.print_name()
print C.b # or a.b
Of course, you can also place b on P. In this case, do
def print_name():
P.b.append(datetime.datetime.now())
Is there a way to share member variables between a class and a nested class ?
for example
class Base(object):
class __Internal(object):
def __init__(self, parent):
self.__parent = parent
self.__parent.__private_method()
#
def __init__(self):
self.__internal = Base.__Internal(self)
return
def __private_method(self):
print "hurray"
return
if name == "main":
b = Base()`
is there a way for the __Internal class to access members of the parent class ?
iam looking for private members like parent.__vars/__methods .
I have edited the code to better explain this. running this code throws
AttributeError: 'Base' object has no attribute '_Internal__private_method'
To access privete method, instead of this:
self.__parent.__private_method()
use this:
self.__parent._Base__private_method()
Modified your example:
class Base(object):
class __Internal(object):
def __init__(self, parent):
self.__parent = parent
self.__parent._Base__private_method()
def __init__(self):
self.__internal = Base.__Internal(self)
return
def __private_method(self):
print "hurray"
return
if __name__ == "__main__":
b = Base()
It results in:
hurray
You must use BaseClassName.methodname(self, arguments) or BaseClassName.field
Example (very ugly code):
class Base(object):
some_field = "OK"
class Internal(object):
def __init__(self, parent):
self.__parent = parent
def change_some_field(self):
Base.some_field = "NOP"
def __init__(self):
self.__private = "val"
self.__internal = Base.Internal(self)
def show_field(self):
print self.some_field
def change_some_field(self):
self.__internal.change_some_field()
def main():
a = Base()
a.show_field()
a.change_some_field()
a.show_field()
return 0
if __name__ == '__main__':
main()
You can find a very useful resources at Why are Python's 'private' methods not actually private?