0

What does built-in function type do in context of classes?

Determines which name of any value

11th Feb 2019, 2:28 PM
Charitha Sri
Charitha Sri - avatar
7 odpowiedzi
+ 6
#include <stdio.h> int main() { int i,j,a[6]={'t','h','a','n','k','s'}; for(i=0;i<=5;i++) { for(j=0;j<=5;j++) { if(i==j) printf("%c",a[j]); else printf ("*"); } printf ("\n"); } return 0; }
12th Feb 2019, 7:14 PM
Charitha Sri
Charitha Sri - avatar
+ 5
T👍👍👍👍👍 👍H👍👍👍👍 👍👍A👍👍👍 👍👍👍N👍👍 👍👍👍👍K👍 👍👍👍👍👍S
12th Feb 2019, 6:29 PM
Charitha Sri
Charitha Sri - avatar
+ 4
If you give only 1 arg, it returns arg.__class__ In [1]: int.__class__ Out[1]: type In [2]: type(int) Out[2]: type In [3]: i = 5 In [4]: i.__class__ Out[4]: int In [5]: type(i) Out[5]: int If you give 3 args (name, bases, attrs), it creates a new type In [10]: cls = type('name', (object,), {'a': 5}) #This is equivalent to #class name(object): # a = 5 In [11]: cls Out[11]: __main__.name In [12]: type(cls) Out[12]: type In [13]: type(cls()) Out[13]: __main__.name In [14]: cls.a Out[14]: 5 In [15]: cls().a Out[15]: 5
11th Feb 2019, 7:37 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
I think you have already done it then post it😁
12th Feb 2019, 6:44 PM
Charitha Sri
Charitha Sri - avatar
+ 2
Type returns the type of whatever you hand over. If you give an int, type will be 'int'. if you give a self-made class itself, type will be 'type'. if you give an instance of your class, you will get the name of that class, because that's the type. You can try these things yourself in Code Playground.
11th Feb 2019, 4:15 PM
HonFu
HonFu - avatar
+ 1
Mert Yazıcı, interesting, I haven't seen this creative side of 'type' yet. Looks a bit like JavaScript, doesn't it?
11th Feb 2019, 7:45 PM
HonFu
HonFu - avatar
+ 1
Now do that pattern with code! 😁
12th Feb 2019, 6:41 PM
HonFu
HonFu - avatar