Situatie
Funcția type() fie returnează tipul obiectului, fie returnează un nou tip de obiect pe baza argumentelor transmise.
Funcția type() are două forme diferite:
# tip cu un singur parametru
tip (object)
# tip cu 3 parametri
tip (name, bases, dict)
Solutie
Pasi de urmat
numbers_list = [1, 2]
print(type(numbers_list))
numbers_dict = {1: ‘one’, 2: ‘two’}
print(type(numbers_dict))
class Foo:
a = 0
foo = Foo()
print(type(foo))
Output
<class ‘list’>
<class ‘dict’>
<class ‘__main__.Foo’>
o1 = type(‘X’, (object,), dict(a=’Foo’, b=12))
print(type(o1))
print(vars(o1))
class test:
a = ‘Foo’
b = 12
o2 = type(‘Y’, (test,), dict(a=’Foo’, b=12))
print(type(o2))
print(vars(o2))
Output
<class ‘type’>
{‘a’: ‘Foo’, ‘b’: 12, ‘__module__’: ‘__main__’, ‘__dict__’: <attribute ‘__dict__’ of ‘X’ objects>, ‘__weakref__’: <attribute ‘__weakref__’ of ‘X’ objects>, ‘__doc__’: None}
<class ‘type’>
{‘a’: ‘Foo’, ‘b’: 12, ‘__module__’: ‘__main__’, ‘__doc__’: None}
Leave A Comment?