+ 5

New object in python

Hi. Can we add new object to our python program? E.g: a= <'a','b','c'> >>> type(a) output: class UserDefined

7th Apr 2020, 11:02 AM
Ramin.RX7
Ramin.RX7 - avatar
17 ответов
7th Apr 2020, 11:15 AM
Sousou
Sousou - avatar
+ 4
what the hell are u doing there???? It seems to be a cool hack
7th Apr 2020, 11:51 AM
Oma Falk
Oma Falk - avatar
+ 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?
7th Apr 2020, 12:01 PM
HonFu
HonFu - avatar
+ 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. 😁
7th Apr 2020, 1:09 PM
HonFu
HonFu - avatar
+ 4
Folks I like this question very much but forgot to upvote. 😉
7th Apr 2020, 1:48 PM
Oma Falk
Oma Falk - avatar
+ 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.
7th Apr 2020, 1:45 PM
Tibor Santa
Tibor Santa - avatar
+ 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.
7th Apr 2020, 1:05 PM
HonFu
HonFu - avatar
+ 1
thank you Sousou and HonFu . But I knew these trick. I just wanted to make a new type of object that can make it simple like lists with another type name.
7th Apr 2020, 1:00 PM
Ramin.RX7
Ramin.RX7 - avatar
0
But in code dont use the tags '< >'
7th Apr 2020, 11:20 AM
Sousou
Sousou - avatar
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
8th Apr 2020, 10:07 AM
Iyad Ahmed
Iyad Ahmed - avatar
0
Yes this class is over viewing the pass lreanson
8th Apr 2020, 1:53 PM
Dibble Gee
Dibble Gee - avatar
0
is it a question? please provide your tips as a block. dont post in QNA section. Have a nice coding.
8th Apr 2020, 2:31 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
0
S bro it is easy good idea
9th Apr 2020, 9:05 AM
Rahul