Home > Software engineering >  python, write mixed value to class/instance
python, write mixed value to class/instance

Time:11-21

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.

CodePudding user response:

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)

CodePudding user response:

Here are some examples of naming the identifiers in python

num1
FLAG
get_user_name
userDetails
_1234 

In your case, It should be something like this: _10M2_12AAA3BC42. (you can't use hyphe- anywhere in the name and name should not begin with number)

  • Related