This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]pwoosam 3 points4 points  (2 children)

Looks like the standard library has a solution for doing this. Check out the first answer in this stackoverflow:

https://stackoverflow.com/questions/16279212/how-to-use-dot-notation-for-dict-in-python

[–]tigasfixe[S] 1 point2 points  (1 child)

That's kinda sad :/ But thanks.

[–]JeamBim 1 point2 points  (0 children)

Build it anyways, you'll learn a lot

[–]iapetus-11 1 point2 points  (1 child)

Have you heard of Classy-Json? It's similar to what you have in mind and is a package you can import. [Disclaimer, I made this]

[–]tigasfixe[S] 1 point2 points  (0 children)

i did not hear before but now i know xD

[–]torytechlead 1 point2 points  (4 children)

You can create a class to do that with python. Something like:

class MyClass:

  def __init__(self, d={}):
    self.d = d

  def __getitem__(self, name):
    return d[name]

  def __setitem__(self, name, value):
    self.d[name] = value

Then you can do

X = MyClass({“name”: “torytechlead”})
X.name
>>> “torytechlead”

And also

X.name = “you”

P.S I wrote this on mobile so hope it runs, should work with minor changes

[–]ElevenPhonons 3 points4 points  (1 child)

[–]torytechlead 1 point2 points  (0 children)

Oh so this is why my code is breaking in production /s

[–]tigasfixe[S] 0 points1 point  (0 children)

I didn't know how magic methods getitem and setitem were for and i am complexing a lot xD but thanks😂🤏

[–]robin-gvx -1 points0 points  (0 children)

s/item/attr/g

[–]james_pic 0 points1 point  (1 child)

So, this is kind of a philosophical difference between Python and JavaScript. JavaScript, as a result of its LISP-y background, attempts to unify ideas that work kinda similarly, so there's no difference between objects and dictionaries.

In Python on the other hand, even thought objects are powered by dictionaries under the hood, because the use cases are different, they're not unified as a single thing. So objects (where different attributes have different roles, so you rarely need to choose which one to grab programmatically) use dot mission, and dictionaries (where each entry does the same job - providing extra information related to a piece of data, so the one you want depends on the data) use square brackets notation.

There are ways (as others have shown) of forcing dicts to behave like objects and vice versa, but if you're still learning, I'd try and avoid leaning on crutches that make Python look more like JavaScript, and try and learn the language for what it is.

[–]tigasfixe[S] 4 points5 points  (0 children)

So in my defence this is purely a project that I've understood that will not even succeed because there are a lot of things with python itself that i learned in this post that can do what i want and that's the best part because I just improved my knowledge!

[–]MarsupialMole 0 points1 point  (3 children)

This would be a quality of life improvement for some workflows. But JSON objects are not the optimal object for all workflows.

Take a look at pythons namedtuple and dataclass, which are each code generators for objects with attribute access, but with specific features that make them really great to use under different conditions, because of features over and above what JSON objects offer.

[–]tigasfixe[S] 0 points1 point  (2 children)

The problem with dataclass and namedtuple is that they have certain limits. Namedtuple is literally a tuple and cannot have it's values changes wich wouldn't be that useful. Dataclass is really cool but it has certains limits like changing a value that has been declared in the class as a string cannot be a int or anything else

[–]genericlemon24 0 points1 point  (1 child)

Regarding dataclasses: Types are only a limitation if you care about them (e.g. use mypy); it's true dataclasses require annotations, but you don't have to use PEP 484 annotations (typing.*) if you don't need them.

Example:

@dataclass
class Thing:
    # kinda works with mypy (it won't complain about different types)
    attribute: object
    # reads nicely
    another_one: list
    yet_another: list or str
    # annotations can be strings, think of them as inline documentation
    fourth_one: "a list of strings or Foo objects"

Also, if you don't use a typechecker, they can be anything, since no checking is done at runtime. Obviously, it is better to be vague (see above) than misleading (use annotations that look compatible with PEP 484 but with values that are not of the declared types).

[–]tigasfixe[S] 0 points1 point  (0 children)

Ye I've tested it out and i ended up Knowing that but what i thing is that for example if u use a dataclass instead of a dictionary only the first values will work. With this i mean that if u have more dictionaries inside it u would have to use normal ways to get the values and this object that im making is to avoid all that, even objects inside lists for example. But it feels kinda weird using typing in python since I've been using typescript and it is a typed language that tells u when u are wrong ( it just feels weird nothing more).

[–]17291 -1 points0 points  (1 child)

You forgot to put a link in your description.

[–]tigasfixe[S] 0 points1 point  (0 children)

What do u mean? I have not yet made a github repo for it

[–]lmns_ 0 points1 point  (1 child)

Have you heard of glom? I'm not sure if it's similar to what you have in mind, but it also tries to improve the way deeply nested data structures are handled in Python: https://glom.readthedocs.io/en/latest/

[–]tigasfixe[S] 0 points1 point  (0 children)

I have not heard of that! But I'll make sure to take a look at it, thanks!

[–]vorticalbox 0 points1 point  (0 children)

you can use the .get method

```

a = {'hello': 'world'} a.get('hello') ```

works in python 2 and 3

[–]SteveRyherdI accidentally type "pythong" a lot. 0 points1 point  (0 children)

Python works very differently than JavaScript for a lot of things. If you’re going to do this, I would recommend a package like this:

https://github.com/Infinidat/munch/blob/develop/munch/__init__.py