0

I am new to Python, and I want to know if there is a way to make the age value calculated using the year of birth which is an item with the age in the same dictionary.

This is what it came to my mind, and I think there is simple way like this without using additional variables of functions.

person = {
          'name': Jane,
          'yearofbirth': 1995,
          'yearnow': 2019,
          'age': person['yearnow'] + person['yearofbirth']
        }

Any help would be appreciated. Thank you!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Mourad gh
  • 23
  • 4
  • 2
    How does addition (`+`) give age? You should subtract (`-`). – Austin Apr 28 '19 at 16:27
  • 1
    You can't reference something that's yet to become an object yet... so the `person['yearnow']` etc... can't work.... Since you already have 2019 and 1995, presumably from somewhere, can't you just use that, that or... set it afterwards? Also... what'd you expect to happen if someone suddenly did `person['yearofbirth'] = 1994` ? – Jon Clements Apr 28 '19 at 16:37

3 Answers3

0

Yes, you can Just not decalre the whole dict in one act

person = {
          'name': Jane,
          'yearofbirth': 1995,
          'yearnow': 2019
         }
person["age"] = (lambda yearnow, yearofbirth: yearnow - yearofbirth)(**person)

But in your example you shouldn't change anything, because there is no way to simplify it(easily). My solution should be used only in complicated tasks. I just you the way to simplify it in case of a huge amount of values in dict.

sakost
  • 250
  • 4
  • 15
0

Instead of hardcoding the current year, you could get python to give it to you

from datetime import datetime

currentYear = datetime.now().year


person = {
          'name': 'Jane',
          'yearofbirth' : 1995

        }

age = currentYear - person.get( "yearofbirth", "") 
person.update({'age': age})

print(person)

You can't set your age inside the dict, as it has not been defined yet.

If you do like above code, we are setting the age outside the dict, then updating the dict with the value we just calculated based on currentYear and the age

The output is:

{'name': 'Jane', 'yearofbirth': 1991, 'age': 24}
sykezlol
  • 88
  • 9
-2

You may have self referential dictionary by using a class derived from dict but dict itself doesn't have this capability. The following code is taken from this answer.

class MyDict(dict):
   def __getitem__(self, item):
       return dict.__getitem__(self, item) % self

dictionary = MyDict({

    'user' : 'gnucom',
    'home' : '/home/%(user)s',
    'bin' : '%(home)s/bin' 
})


print dictionary["home"]
print dictionary["bin"]
ramazan polat
  • 7,111
  • 1
  • 48
  • 76
  • 2
    subclassing dicts(or any built in type obj) is a BAD IDEA, an empty dict is falsy, a derived empty dict is truthy, you'd have to reimpliment alot, they're not supposed to be subclassed. – Aditya Shankar Apr 28 '19 at 16:34
  • @AdityaShankar I completely agree with you, while my sole intent was to answer the question without touching it's usefulness. But your valuable comment is also should be noted. – ramazan polat Apr 28 '19 at 16:38
  • 2
    @AdityaShankar not always a bad idea... just not necessarily the right choice... subclassing from `collections.UserDict` is probably where you want to go most of the time, but sometimes subclassing directly from `dict` is what's needed (depends on how much you might need to override etc... - if it's just 1/2 functions and the rest is fine, then subclass directly, if it's a lot, then use the mixins) – Jon Clements Apr 28 '19 at 16:47