0xAX
0xAX

Reputation: 21817

Create new class object

I have 2 class in python cl1 in f1.py file and cl2 in f2.py file. I wrote import f2

import f2

class cl1:
  a = f2.cl2()

But i see error in a = f2.cl2(): module object has no attribute 'cl2'

Why?

Thank you.

Upvotes: 0

Views: 702

Answers (2)

Alex Martelli
Alex Martelli

Reputation: 881497

The following code works just fine (if you're using Python 3 you can omit the (object) parts, but in Python 2 you should leave them in -- they're not responsible for your bug, but if you get used to omitting them you'll have strange problems in the future as your code grows...):

f2.py is:

class cl2(object):
  pass

f1.py is:

import f2

class cl1(object):
  a = f2.cl2()

If your code is not working, it must be different from this. Please confirm that this simple code is working for you, then show us (by editing your original question, not by posting comments or "answers") how your non-working code differs (lack of imports, circular imports, wrong imports, or whatever else).

Upvotes: 0

ptikobj
ptikobj

Reputation: 2710

sorry, i was wrong: your problem is probably that you have a circular import: f1 imports f2 and vice versa. check your design, as it usually should be possible to design your software without a circular import.

see: this

Upvotes: 1

Related Questions