Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

How to access a function inside?

hello,

Good morning, I have this python function on file called base_model.py
Code
def to_dict(self):
       """ : returns a dictionary all keys/values of dict of the instance"""
       ret_dict = self.__dict__.copy()
       ret_dict["created_at"] = self.created_at.isoformat()
       ret_dict["updated_at"] = self.updated_at.isoformat()
       ret_dict["__class__"] = self.__class__.__name__
       return ret_dict

here an example for  accessing to function to_dict inside.
Code
>>>from base_model import BaseModel
>>> dic = BaseModel()
>>> mdic = dic.to_dict()
>>> print(dic)  #prfint  everything the funcion inside
>>>
[BaseModel] (d130a170-ead3-4942-b01c-caffdd688e34) {'id': 'd130a170-ead3-4942-b01c-caffdd688e34', 'created_at': datetime.datetime(2019, 11, 9, 10, 51, 30, 386593), 'updated_at': datetime.datetime(2019, 11, 9, 10, 51, 30, 386607)}


Code
>>>print(mdic) #print created_at  and updated_at y the class name
{'id': 'd130a170-ead3-4942-b01c-caffdd688e34', 'created_at': '2019-11-09T10:51:30.386593', 'updated_at': '2019-11-09T10:51:30.386607', '__class__': 'BaseModel'}

My question is: how  can I acces  to updated_at  or  to created_at?

Thanks
Code
print(mdic['created_at'])
print(mdic['updated_at'])
(Nov 9, 2019 10:33 AM)Kallu609 Wrote:  
Code
print(mdic['created_at'])
print(mdic['updated_at'])

Thanks, yes, it worked


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by FhackMar 2, 2022 03:52 AM 4 307 Mar 2, 2022 03:52 AM
Last Post: Fhack
[C] by Gliese 667CcAug 28, 2021 11:08 AM 3 192 Aug 28, 2021 11:08 AM
Last Post: Sierra-117
[C] by jammy47Mar 17, 2017 09:40 AM 3 280 Mar 17, 2017 09:40 AM
Last Post: Goodies