반응형

 

class, type

http://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/#types-vs-classes

> in python2

> types (classes implemented in C)

> classes (implemented in pure Python)

>

> in Python 3 class and type mean the same thing.

> A class is an object, and just like any other object,

> it's an instance of something: a metaclass. The default metaclass is type.

> 그래?

> 그러네.. isinstance(클래스명, type) 하면 True 나온다.

> 클래스도 인스턴스라는거네.. 재밌네

x.__class__

type(x)


>>> type(3)
<type 'int'>
>>> type('a')
<type 'str'>
>>> type([1,2,3])
<type 'list'>
>>>

<type 'list'>
>>> 3.__class__
  File "<stdin>", line 1
    3.__class__
              ^
SyntaxError: invalid syntax
>>> 'a'.__class__
<type 'str'>
>>> [1,2,3].__class__
<type 'list'>
>>>

아래는 내가 해본건데 신기하네.. instance랑 아닌거랑 type 또는 __class__의 결과가 다르군


class Foo(object):
    pass

f = Foo()

print Foo.__class__
print type(Foo)

print f.__class__
print type(f)

결과
<type 'type'>
<type 'type'>
<class '__main__.Foo'>
<class '__main__.Foo'>

반응형

+ Recent posts