5

I have defined some classes thusly:

class CustomParameter():
    def __init__(self, strFriendlyAttribName, strSystemAttribName):
        self.FriendlyAttribName = strFriendlyAttribName
        self.SystemAttribName = strSystemAttribName

class PartMaster():
    AttribNameList = ["Part Number", "Name", "Standard Part", "Part Type", "ControlledBy", "PIN", "Design Responsibility"]

    def __init__(self):
        self._UUID = None

        self.PartNumber = CustomParameter("Part Number", "V_ID"),
        self.Name = CustomParameter("Name", "V_name"),
        self.StandardPart = CustomParameter("Standard Part", "V508_isStandardPart"),
        self.PartType = CustomParameter("Part Type", "V511_PartType"),
        self.ControlledBy = CustomParameter("ControlledBy", "V511_ControlledBy"),
        self.PIN = CustomParameter("PIN", "BOECACPinItemNumber"),
        self.DesignResponsibility = CustomParameter("Design Responsibility", "BOECACDesignRpnse")

class Part():
    def __init__(self, PartNumber):

        self.PartNumber = PartNumber

        #This instance wraps
        self.PartMaster = PartMaster() #create new instance



test = Part("ABC")

I would expect that test.PartMaster.PIN would be an instance of CustomParameter, but instead it is a tuple tuple: (<__main__.CustomParameter instance at 0x0000000002D724C8>,) Why is this, and how can I make it not be so?

I'd like to construct my classes such that test.PartMaster.PIN gives me back the instance instance of my CustomParameter class. Any ideas?

1 Answer 1

8

Because your instance vars of the PartMaster class are set with commas at the end whenever your class is initialized :)

Python interprets this:

x = 'test',

as:

('test',)

Try this instead:

def __init__(self):
    self._UUID = None

    self.PartNumber = CustomParameter("Part Number", "V_ID")
    self.Name = CustomParameter("Name", "V_name")
    self.StandardPart = CustomParameter("Standard Part", "V508_isStandardPart")
    self.PartType = CustomParameter("Part Type", "V511_PartType")
    self.ControlledBy = CustomParameter("ControlledBy", "V511_ControlledBy")
    self.PIN = CustomParameter("PIN", "BOECACPinItemNumber")

This has happened to me a bunch of times. Whenever I switch from writing out a ton of dictionaries to setting vars in classes like this I always forget about the comma. But pull your hair out over it once and you'll always know what to look for!

3
  • 1
    Aww sphincters. Thanks for the assist!
    – nickvans
    May 6, 2016 at 5:33
  • @nickvans Please accept this as an answer for others to find if it helped you out!
    – tknickman
    May 6, 2016 at 5:39
  • 1
    Sorry! It was non obvious on my phone... Because I want paying attention. Again, thanks a million!
    – nickvans
    May 6, 2016 at 5:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.