access variable out of class - python

I want to access username variable in class b who created in class a.
class a():
def __init__(self):
self.username = "amirhossein"
self.passwd = 1234
class b():
def __init__(self):
self.amir = a.username
print(self.amir)
b()
this code give me this error:
AttributeError: type object 'a' has no attribute 'username'
what should I do?

In order to access variables that are in a class, you either need to make them class variables, or create an instance of that class and access that instance's variables.
Here's how you'd use class variables:
class a():
username = "amirhossein"
passwd = 1234
def __init__(self):
pass
class b():
def __init__(self):
self.amir = a.username
print(self.amir)
b()
And here's how you could do it with instances:
class a():
def __init__(self):
self.username = "amirhossein"
self.passwd = 1234
class b():
def __init__(self, a_instance):
self.amir = a_instance.username
print(self.amir)
b(a())
Both of them have the same output - amirhossein.
Helpful reading: Class vs Instance Variables

Related

python mock class instance in unittest

I do not see the actual mock object created for the class instance which I am trying to mock in the following code.
class_to_be_mocked.py
class ClassToBeMocked():
def __init__(self):
pass
def method(self):
return "abc"
class_under_test.py
class ClassUnderTest():
def __init__(self):
self.api = ClassToBeMocked()
def method(self):
return self.api.method()
test.py
class TestClass(unittest.Testcase):
def setUp(self):
with patch('class_to_be_mocked.ClassToBeMocked') as mocked_class:
self.mocked_class_instance = mocked_class.return_value
def test_method(self):
instance = ClassUnderTest()
self.mocked_class_instance.method.return_value = "abc"
instance.method()

How can I pass a variable from one class to another in Python?

I'm creating a little stock management app and I want to access a variable from ClassA in ClassB
ClassA(Screen):
def test1(self):
self.username = "Tonny"
ClassB(Screen):
def test2(self):
print(ClassA.test1.username)
This code dont work, i already tried like this:
ClassA(Screen):
def test1(self):
self.username = "Tonny"
return self.username
ClassB(ClassA,Screen):
ClassA = ClassA()
def test2(self):
print(ClassA.test1())
The second piece of code should work, but I can't use it because of the screen manager, it stop working right when I put another argument in the class.
Does anyone know how can I do this?
self.x means a variable x just works inside the class definition.
Try this: Define a class A(object), define x=1 before init()
class A(object):
x = 1
__init__(self)
.....
You can address x like A.x.
You can do it by sending the instance of ClassA to ClassB in order for ClassB to access it:
class ClassA(object):
def __init__(self):
self.username = "Tonny"
class ClassB(ClassA):
def __init__(self, class_a):
self.username = class_a.username
def getPassedName(self):
return self.username
object1 = ClassA()
object2 = ClassB(object1)
username = object2.getPassedName()
print(username)
For more info check this question.

Python: Access base class "Class variable" in derive class

I am trying to access a class variable from the base class in the derived class and I am getting a no AttributeError
class Parent(object):
variable = 'foo'
class Child(Parent):
def do_something(self):
local = self.variable
I tried using it as Parent.variable but that did not work either. I am getting the same error
AttributeError: 'Child' object has no attribute 'Child variable'
How do i resolve this
I'm not sure what you're doing wrong. The code below assumes you have an initialization method, however.
class Parent(object):
variable = 'foo'
class Child(Parent):
def __init__(self):
pass
def do_something(self):
local = self.variable
print(local)
c = Child()
c.do_something()
Output:
foo
The code shown below should work on both Python 2 & 3:
class Parent(object):
variable = 'foo'
class Child(Parent):
def do_something(self):
local = self.variable
c = Child()
print(c.variable) # output "foo"

How to contain an object and call its method in a class?

I have two classes (ClassA and ClassB) and ClassA contains one object, b, that is an instance of ClassB. The question is that I can't call the b's method in Class A.
class ClassB(object):
def __init__(self):
print('Class B init ...')
def show(self):
print('Showing class b')
class ClassA(object):
#__classb = ClassB()
def __init__(self, classb):
print('Class A init ...')
__classb = classb
def show(self):
__classb.show() # <=== I just want to do this!
b = ClassB()
a = ClassA(b)
a.show()
I expect the result should be:
Class B init ...
Class A init ...
Showing class b
But I meet the problem as this image shows:
How can I fix it?
By doing __classb = classb you are only defining a local __classb variable in the __init__ method.
If you want __classb to be an instance attribute you will need to use self:
self.__classb = classb
And then:
def show(self):
self.__classb.show()
You should create a attribute for a instance of class B in class A like that self.__classb.
Following code
class ClassB(object):
def __init__(self):
print('Class B init ...')
def show(self):
print('Showing class b')
class ClassA(object):
def __init__(self, classb):
print('Class A init ...')
self.__classb = classb
def show(self):
self.__classb.show() # <=== I just want to do this!
b = ClassB()
a = ClassA(b)
a.show()

How do I access instance data from a method in python?

So I am trying to create a public method that can be called by class a so that it edits a data item in class b.
class aClass():
def __init__(self):
aVariable = 1
class aNotherClass():
def aMethod(self):
aFunction(5)
def aFunction(aNumber):
instance1.aVariable = aNumber
instance1 = aClass()
instance2 = aNotherClass()
instance2.aMethod
However, when I call instance1 in aFunction, python tells me it isn't defined. If I want to change aVariable in aClass, what should aFunction() say?
I think you may forget the self when you define a class method.
refer to: https://docs.python.org/2/tutorial/classes.html#class-objects
class aClass():
def __init__(self):
aVariable = 1
class aNotherClass():
def aMethod(self):
aFunction(5)
def aFunction(aNumber):
instance1.aVariable = aNumber
instance1 = aClass()
instance2 = aNotherClass()
instance2.aMethod()

Categories