1

my class structure is something like this.

class A():
    def __init__(self):
        self.matched_condition_set = set()

    def add_to_matched_condition(self,condition):
        self.matched_condition_set.add(condition)

class B():
    def __init__(self, list_of_A):
        self.list_of_a = list_of_A

    def do_stuff(self):
        for a in self.list_of_a:
            if helper.check_for_a(a):
                print(a.matched_condition_set)

in a file called helper.py i have the following function.

def check_for_a(a):
    print(type(a))
    a.add_to_matched_condition("hello")
    return True

Now if i call class B object, I get that:

A instance has no attribute 'add_to_matched_condition'

. also when I try to get the type of a >> print(type(a)) inside the helper method. i get type<'instance'>. where is the object getting lost ?

2
  • What do you mean call class B object? Commented Dec 8, 2014 at 4:54
  • Works for me - a = A(); b = B([a]); b.do_stuff() results in <type 'instance'> and set(['hello']). Commented Dec 8, 2014 at 4:59

2 Answers 2

2

I don't know why you're getting A instance has no attribute 'add_to_matched_condition'; your code works ok for me on Python 2.6.4

To get a more useful type signature (plus other goodies) on your classes you need to make them inherit from object.

Here's my slightly modified version of your code that illustrates that; it also shows how I tested your classes.

#!/usr/bin/env python

import helper

class A(object):
    def __init__(self):
        self.matched_condition_set = set()

    def add_to_matched_condition(self,condition):
        self.matched_condition_set.add(condition)

class B(object):
    def __init__(self, list_of_A):
        self.list_of_a = list_of_A

    def do_stuff(self):
        for a in self.list_of_a:
            if helper.check_for_a(a):
                print(a.matched_condition_set)


a1 = A(); a2 = A()
b = B([a1, a2])

print a1, a2
print b, b.list_of_a

b.do_stuff()

print a1.matched_condition_set 
print a2.matched_condition_set 

output

<__main__.A object at 0xb758f80c> <__main__.A object at 0xb758f84c>
<__main__.B object at 0xb758f86c> [<__main__.A object at 0xb758f80c>, <__main__.A object at 0xb758f84c>]
<class '__main__.A'>
set(['hello'])
<class '__main__.A'>
set(['hello'])
set(['hello'])
set(['hello'])
Sign up to request clarification or add additional context in comments.

Comments

1

The reason you are seeing type<instance> is most likely that you are using Python 2.x. Because you are defining your classes as

class A():

instead of

class A(object):

you are creating "old style" classes.

As to your other problem, we'd need to see the code in which you create a list of A()s and pass that list into the constructor for B(). Please add that code if you want an answer to that part of your question.

Also, it's unclear to me why check_for_a is not a method of A(), and I'm not sure I would name a function check_for_a if it actually has the side-effect of altering the object it's checking.

2 Comments

declaring a class to class A(object): solved it. but what is the difference between the two types ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.