Search code examples
pythonclassobjectvariablesinstance

python, write mixed value to class/instance


i have a class which is called "devices" incl. two attributes e.g "device_name" and "serial_number". unfortunately i need to create instance names like "10M2-12AAA3BC42" (mixed values, string & numbers) The lengths of the names differs.

I can create the instance "10M2-12AAA3BC42". But if i call "10M2-12AAA3BC42"

print(10M2-12AAA3BC42.serial_number)

i'm getting an "invalid syntax".

Is there a way to use mixed values as described above?

many thanks in advance.


Solution

  • Your naming identifiers is not correct. You might try below to access attributes

        class devices:
            def __init__(self, device_name , s_no):
                self.device_name = device_name
                self.s_no = s_no
    
        _10M2_12AAA3BC42 = devices('10M2', '12AAA3BC42')
    
        print(_10M2_12AAA3BC42.s_no)
        print(_10M2_12AAA3BC42.device_name)