Python Notes -Random points

Tags

>>> a = 2
>>> a.__class__
<type 'int'>

>>> all([])
True
>>> range(2,2)
[]

>>> string1 =  'mayank'
>>> def foo():
	string1 = 'mac'
	print string1
	print locals()

>>> print globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', 'foo': <function foo at 0x020BE030>, '__doc__': None, 'string1': 'mayank'}
>>>
>>> foo()
mac
{'string1': 'mac'}
>>> string1
'mayank'

>>> issubclass(str,object)
True
>>> def foo():
        pass
>>> issubclass(foo.__class__, object)
True

>>> class Teacher(School):
	pass

>>> issubclass(Teacher, School)
>>> issubclass(School, Teacher)
False
>>> class School(object):
	pass

>>> issubclass(School, object)
True
>>> School.__class__
<type 'type'>
>>> type(School)
<type 'type'>
>>> type(object)
<type 'type'>

>>> class School:
	pass

>>> School.__mro__

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    School.__mro__
AttributeError: class School has no attribute '__mro__'
>>> class School(object):
	pass

>>> School.__mro__
(<class '__main__.School'>, <type 'object'>)
>>> class Teacher(School):
	pass

>>> Teacher.__mro__
(<class '__main__.Teacher'>, <class '__main__.School'>, <type 'object'>)

>>> x = 10
>>> def foobar():
	print x
	x += 1

>>> foobar()

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    foobar()
  File "<pyshell#4>", line 2, in foobar
    print x
UnboundLocalError: local variable 'x' referenced before assignment

>>> x = 10
>>> def foobar():
	global x
	print x
	x += 1

>>> foobar()
10
>>> foobar()
11