0

I have a package containing a module called rigfuncs3.py

SDNpackage/
    __init__.py
    rigfuncs3.py

Code:

class rig():

    def __init__(self, rigNumber, leftNozPlate, rightNozPlate, bottomNozPlate, row1_centre_focus):

        self.rigNumber = rigNumber
        self.leftNozPlate = leftNozPlate
        self.rightNozPlate = rightNozPlate
        self.bottomNozPlate = bottomNozPlate
        self.row1_centre_focus = row1_centre_focus

    def getRigName(self):
        return self.rigNumber

A script called play_04.py imports the module rigfuncs from the package. Code:

from SDNpack2 import rigfuncs3

instantiation = rigfuncs3.rig(1,1000.0, 2000.0, 3000.0, 4000.0)
print(dir(instantiation))

rig_num = instantiation.getRigName()

When run play_04 I get the error:

AttributeError: 'rig' object has no attribute 'getRigName'

I have no idea why, any ideas?

5
  • 1
    I'm failing to find a problem with your provided code. Are you sure that the class rig is defined in rigfuncs.py? Also can you please update your code so that only the code needed to replicate the problem is provided? Could you also provide a folder-structure for your package? If you're using windows, move into the "root" directory of your package and issue the command tree /f and give us that output as well. Commented Jun 4, 2020 at 8:07
  • The code shown is the total code for both files, I fixed it by adding another import in play_02 from rigfuncs import rig. which fixes that issue but moves me on to another problem. Commented Jun 4, 2020 at 8:28
  • The only way I could see that your problem is occurring is if you're polluting the namespace inside of the package (which seems likely because you've reused rig multiple times). Please read through this help page and update your code accordingly so that we can help you find the root cause. Commented Jun 4, 2020 at 9:59
  • @HampusLarsson thank you I shall read this. Commented Jun 4, 2020 at 13:47
  • @HampusLarsson hi, I have read the doc and totally changed the question to relflect the simplifiedcode and structure. Commented Jun 4, 2020 at 15:02

1 Answer 1

1

I still cannot replicate the problem you're having.

I've tried to copy your stated folder-structure like this:

C:.
│   play_04.py
│
└───SDNpack2
        rigfuncs3.py
        __init__.py     <--- empty file

rigfuncs3.py:

class rig():
    def __init__(self, rigNumber, leftNozPlate, rightNozPlate, bottomNozPlate, row1_centre_focus):
        self.rigNumber = rigNumber
        self.leftNozPlate = leftNozPlate
        self.rightNozPlate = rightNozPlate
        self.bottomNozPlate = bottomNozPlate
        self.row1_centre_focus = row1_centre_focus

    def getRigName(self):
        return self.rigNumber

play_04.py:

from SDNpack2 import rigfuncs3

instantiation = rigfuncs3.rig(1, 1000.0, 2000.0, 3000.0, 4000.0)
print(instantiation)
print(instantiation.__dict__)

rig_num = instantiation.getRigName()
print(rig_num)

I change your call of dir(instantiation) into instantiation.__dict__ to just show the attributes of the object, not all built-in variables.

Output:

<SDNpack2.rigfuncs3.rig object at 0x02B1E628>
{'rigNumber': 1, 'leftNozPlate': 1000.0, 'rightNozPlate': 2000.0, 'bottomNozPlate': 3000.0, 'row1_centre_focus': 4000.0}
1

The only way I can replicate the problem in of itself is if I either change the indentation of getRigName or if I remove it all together from the class.

Your code works the way you have coded it, I see no actual problem here.

Sign up to request clarification or add additional context in comments.

2 Comments

I made a new package with just init.py and rigfuncs3.py and it works too. I’ve accepted your answer because you advised in a comment that it may be the package structure. I didn’t know instantiation.__dict__ gave the attributes - I have no idea what all the private methods do, thank you Hampus your help has helped me learn a lot in the last few days.
@Windy71 When you call dir on an object, you're showing all the "stuff" that is inside of that object. Some of those are sometimes called "magic methods" which change how the object behaves. You can read more about them here. Two of the best ones to try to start implementing of those are __repr__ and __str__ which handles how your class handles calls from repr() and str() respectively. If __str__ is not defined, it then automatically falls back to call __repr__.

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.