17 Answers
+ 10
+ 4
what the hell are u doing there????
It seems to be a cool hack
+ 4
There's a way creating an object on the fly, using the type function, for example like this:
myobj = type('MyType', (object,), dict(a=1, b=2, c=3))()
print(myobj)
print(myobj.a)
That what you're talking about?
+ 4
Oh, wait, do you mean, you want to invent a new *literal* for your type?
So that, just as you can write...
l = []
d = {}
s = ''
... you could write this...
x = <1, 2, 3>
... and it would become an instance of your own type?
That would be similar to operator overload, maybe 'delimiter overload'?
Okay, I think we can't do that.
That would be quite fancy. š
+ 4
Folks I like this question very much but forgot to upvote.
š
+ 3
In python you cannot create your own symbols like that. There is some fancy stuff like the matmul operator @ which is used (implemented) exclusively by numpy. You can override it with magic methods same as other operators. But <> cannot be used like this.
In some programming languages like Lisp or Haskell you have more freedom to define such symbols.
+ 2
You can inherit from builtin types, would that do the trick?
class MyList(list):
...
And then define whatever you want to add or override, everything else remaining just like list.
0
But in code dont use the tags '< >'
0
you just create a new class
thatās what class is a blueprint for objects
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/class
0
Yes this class is over viewing the pass lreanson
0
is it a question? please provide your tips as a block. dont post in QNA section.
Have a nice coding.
0
S bro it is easy good idea