1

I'm trying to create a system on Python that allows me to create a list called (user)total, 'user' being the name of the user before total. However this is subjective as any account with any username could be made within my program.

I have tried to use %stotal = [''] %user however this comes up with a syntax error. How would I manage to do this?

1
  • 5
    Use a dictionary. Don't try to put data into variable names. Dec 17, 2013 at 18:01

2 Answers 2

2

You can't do that kind of meta-programming in python! (not with the syntax you posted) But instead you can create a dictionary of lists indexed by the user name:

total = {}
total['username1'] = [''] #list for this username total
total['username2'] = [''] 

etc.

4
  • 3
    You well can, but in most cases you shouldn't. Dec 17, 2013 at 18:03
  • 1
    ah I see, so then for the usernames I'll be able to use %s and add them to the dictionary in that way? Dec 17, 2013 at 18:14
  • Yes, but that depends of your logic :) Dec 17, 2013 at 18:16
  • I left an explanation in my answer of how you could do it the other way if you really want to, and why it's a bad idea. Dec 17, 2013 at 18:28
0

It is possible. Hopefully seeing how will help to illustrate why, as Hyperboreus says, it's not a good idea.

If you do dir() in your interactive Python environment, you'll get a list of names that are available in your current scope. There will always be one called __builtins__, which exposes all of the functions and constants in the builtins module. These functions and constants are defined to be exactly the same ones that are available right from the start of your Python session in the global namespace, which you can take a look at with the builtin function globals().

In accordance with the Python data model, every Python object has an element named __dict__ that's a dictionary object whose keys are member names. If obj is the name of some Python object in the current scope, obj.__dict__["keyname"] will access the same member that you could get to more simply through obj.keyname.

So putting this together, you can set key/value pairs in __builtins__.__dict__ directly:

>>> __builtins__.__dict__["testvarname"] = "testval"
>>> print testvarname
testval

Whew! Getting pretty abstract pretty quick here. This might be useful for defining behavior based on user input or something else that you might not know until runtime... but you can probably see how you're working through a lot of complexity to get there and sort of circumventing the normal rules that Python sets out to try to help you keep your programs organized and easy to understand. xndrme's answer is likely to be the more straightforward way to solve the bigger problem you're facing.

1
  • 1
    No problem its just funny ;) Dec 17, 2013 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.