2

In Lua script, when i have to wirte too many lines like:

obj.age=17
obj.name="jack"
obj.country="US"
...
obj.weight=100

I can use _ENV to simpify the code:

_ENV=obj
age=17
name="jack"
country="US"
...
weight=100

Namely, I can avoid typing obj. repeatedly, is there a way in python to do like this? I searched on net buf found nothing.

I need to use python script as a configuration file, and the latter format seems better.

weiweishuo
  • 847
  • 7
  • 15
  • 3
    The classic [XY problem](http://xyproblem.info)... "I need to use python script as a configuration file" Does not seem like the best approach. Check `ini`, `json` or `yaml` file formats – DeepSpace Dec 16 '18 at 14:16
  • 1
    Also, take a look to Python configparser https://docs.python.org/3/library/configparser.html, it allows to define and manipulate config files very easily. – Cartucho Dec 16 '18 at 14:34

3 Answers3

3

Why not

obj = {
    'age': 17,
    'name': 'jack',
    'country': 'US',
    ...,
    'weight': 100
}

? Newlines need no \ here, since they are inside a pair of braces.

Of course, this builds a dictionary, but do you really need an object of a specific class in a configuration?

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
  • 1
    If OP does need object access, (s)he could look into [`SimpleNamespace`](https://docs.python.org/3/library/types.html#types.SimpleNamespace), or [`namedtuple`](https://docs.python.org/3/library/collections.html#collections.namedtuple). – Nelewout Dec 16 '18 at 14:35
1

You can easily do it using SimpleNamespace (Python3 only)

import types
obj = types.SimpleNamespace(
    age=17
    name="jack"
    country="US"
    ...
    weight=100
)
luminousmen
  • 1,971
  • 1
  • 18
  • 24
0

You really asked two questions in one.

For the second about storing configuration, you can

  • write your vars in say, a 'config.py' module. Then import config as cfg and manipulate using cfg. qualified names. You can also from config import * for direct use but that's not recommended and you loose write capability in the original config module. They are copied into current namespace, not linked.

  • simply use a dictionary to hold your settings like Walter said. They are the obvious builtin type for that.

  • make your own Configuration class, instantiate cfgand manipulate attributes through cfg. qualifier.
  • use a shelf, a persistant dict with the shelve module.
  • Because shelves use pickle and this is a binary format used mostly in Python (I think)to store objects, you may prefer to use some more human oriented open format like suggested by DeepSpace and Cartucho (see configparser for .iniand so on).
  • Much like with the class case, use a namedtuple or a SimpleNamespace (didn't know this one) like Wouda and luminousmen said.

Probably there are even more choices but I think the list looks enough for most tastes.

Now for the first question, that I think is a very good one, I am almost sure that the answer is no, you can't do that in Python. From here I found this which is the reasoning for self use obligation. Safety, explicitness, and performance it looks. I suppose if it were to be allowed, you could use it too for self, and maybe that's why you can't.

I sometimes miss that feature too.

progmatico
  • 4,714
  • 1
  • 16
  • 27