-3

I am having a function in python

def func(dataFrame,country,sex):
    varible_name=dataFrame[(dataFrame['land']==country) & (dataFrame['sex']==sex)]

Now, for example, I call this function

func(dataFrame,'England','M')

I want that variable name be England_M instead of variable_name.

10
  • 7
    No, you certainly do not want that. Commented Feb 5, 2018 at 12:34
  • 1
    Why do you want to do this? That is a bad design pattern. You can get close to this by using a dictionary; mydict = {England_M: myvalue}. That gives you almost all the advantages of what you ask without the disadvantages. Commented Feb 5, 2018 at 12:34
  • 2
    Use a dictionary (by the way, this is a legitimate question, so just ignore those dislikes). Commented Feb 5, 2018 at 12:35
  • 1
    If you really want an underscore, try something like: my_dict['{}_{}'.format(country, sex)] = ... Commented Feb 5, 2018 at 12:39
  • 2
    @Martin Why so complicated? my_dict[(country, sex)] = ... Commented Feb 5, 2018 at 12:40

1 Answer 1

1

You can't do that in Python. What you can do instead is store the results under a dictionary with key = England_M for instance.

In your case, you could do the following :

def func(dataFrame,country,sex):
    tmp = dataFrame[(dataFrame['land']==country) & (dataFrame['sex']==sex)]
    variable_name = "{c}_{s}".format(c=country, s=sex)
    return dict(variable_name=tmp)

Now using it :

results = func(dataFrame, "England", "M")
print(results['England_M'])
Sign up to request clarification or add additional context in comments.

6 Comments

you can but you shouldn't
@Ev.Kounis yea like that locals()['my_var_name']='some_vale'
@Ev.Kounis oh wow, didn't even know about that. Yep, doesn't seem like a safe option.
Thanks folks for suggestions. It seems these work arounds should work.
Yes, you are indeed right. I agree. My apologies! Thanks a lot for your inputs.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.