Om Prakash Kumar
Om Prakash Kumar

Reputation: 61

How to use object as value in dictionary in python?

Here's the code:

import datetime
local = {};
class name:
     x = 0
     y = 0
     time = 0
     PCH = []
     FCC_Queue = []

t = datetime.time(0, 0, 0)
p = name()
p.x = 10
p.y = 20.0
p.time = t.second 
PCH.append('xyz','abc',1,15.0)
FCC_Queue.append(10.0,20.0,30.0)
local['Obj1'] = p
  1. How do I access the value of p.x from the dict local['Obj1']?
  2. Also, how do I access a list value e.g. PCH[1] from the dict local['Obj1']?

Upvotes: 2

Views: 781

Answers (1)

Francesco
Francesco

Reputation: 4250

Of course you can and your code works.

To access the x of your object is as simple as

localhostrecord['Obje1'].x

To access the list you do the same and then treat this as a simple list

localhostrecord['Obje1'].PCH
# e.g. access second element of PCH list
localhostrecord['Obje1'].PCH[1]

Upvotes: 2

Related Questions