0
What does built-in function type do in context of classes?
Determines which name of any value
7 ответов
+ 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;
}
+ 5
T👍👍👍👍👍
👍H👍👍👍👍
👍👍A👍👍👍
👍👍👍N👍👍
👍👍👍👍K👍
👍👍👍👍👍S
+ 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
+ 4
I think you have already done it then post it😁
+ 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.
+ 1
Mert Yazıcı, interesting, I haven't seen this creative side of 'type' yet.
Looks a bit like JavaScript, doesn't it?
+ 1
Now do that pattern with code!
😁