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?
The nested tuple with the elements (100, 200, 300) can be retrieved by using tuple name with the index value i.e. tup[index] and each element of the nested tuple can be accessed by using tup[index-1][index-2].
Deleting a Tuple As discussed above, we cannot change the elements in a tuple. It means that we cannot delete or remove items from a tuple. Deleting a tuple entirely, however, is possible using the keyword del.
The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified. Tuples are more memory efficient than the lists.
Creating a Tuple A tuple in Python can be created by enclosing all the comma-separated elements inside the parenthesis (). Elements of the tuple are immutable and ordered. It allows duplicate values and can have any number of elements.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With