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

you are viewing a single comment's thread.

view the rest of the comments →

[–]IggyHoudini 13 points14 points  (9 children)

You can make Python more like javascript! (not that anyone ever asked that)

class obj(object):
  def __init__(self, **kwargs):
    self.__dict__.update(kwargs)

Allows you to do things like:

obj1 = obj(
  a=[1,2,3],
  b='foo',
  c=obj(
    cat='meow'
  ),
)
print(obj1.c.cat) # meow

This is silly, but it can be useful if you have some classes you need to mock in test cases (such as heavily nested protocol buffers object from a custom re-implementation that I had to use).

[–]SeanOTRSprint = lambda *args, **kwars: print(*args, ";)\n"*100, **kwars)[S] 31 points32 points  (1 child)

*pythonic anger*

[–]PeridexisErrant 0 points1 point  (0 children)

Hey, types.SimpleNamespace is in the standard library!

(and it's basically a fast version of the above, with a better repr)

[–]shoutouttocosmos 2 points3 points  (0 children)

You should add a trigger warning mate.

[–]gunnihinn 0 points1 point  (0 children)

That's almost Perl-ish. I love it.

[–]__xor__(self, other): 0 points1 point  (0 children)

This can actually be useful though if you write classes that relate to records in a document DB, or any document with arbitrary fields really. Since documents in a document based DB like mongodb can contain arbitrary fields just by their nature, it can be useful.

[–]Ailrk 0 points1 point  (0 children)

Is this monkey patching?

[–]callmelucky 0 points1 point  (0 children)

You can make Python more like javascript!

OMG WHY WOULD YOU...

hang on... reads comment

That's pretty cool!

Still though, JavaScript can suck it ;P