Skip to content

Instantly share code, notes, and snippets.

@cocoa1231
Created August 10, 2017 08:55
Show Gist options
  • Save cocoa1231/445d138dd0dc3db30231691f9e5e792c to your computer and use it in GitHub Desktop.
Save cocoa1231/445d138dd0dc3db30231691f9e5e792c to your computer and use it in GitHub Desktop.
In [1]: def x(object):
...: def __init__(self):
...: self.x = 12
...:
In [2]: print(x.x)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-a981fbbe4431> in <module>()
----> 1 print(x.x)
AttributeError: 'function' object has no attribute 'x'
In [3]: class x(object):
...: def __init__(self):
...: self.x = 12
...:
In [4]: print(x.x)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-a981fbbe4431> in <module>()
----> 1 print(x.x)
AttributeError: type object 'x' has no attribute 'x'
In [5]: class x(object):
...: x = 12
...: def __init__(self):
...: self.x = 12
...:
...:
In [6]: print(x.x)
12
In [7]: print(x.__init__.x)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-49b4f6efc396> in <module>()
----> 1 print(x.__init__.x)
AttributeError: 'function' object has no attribute 'x'
In [8]: class x:
...: def __init__(self):
...: self.y = 5
...: z = 1
...:
In [9]: a = x()
In [10]: x.y
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-24e477bb360e> in <module>()
----> 1 x.y
AttributeError: type object 'x' has no attribute 'y'
In [11]: print(a.y)
5
In [12]: class x(object):
...: def __init__(self):
...: self.y = 5
...: z = 1
...:
In [13]: a = x()
In [14]: a.y
Out[14]: 5
In [15]: class x(object):
...: def __init__(self):
...: self.x = 12
...:
In [16]: class x(object):
...: def __init__(self):
...: self.s = 12
...:
...:
In [17]: x.s
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-035fe37d29c3> in <module>()
----> 1 x.s
AttributeError: type object 'x' has no attribute 's'
In [18]: a = x()
In [19]: a.s
Out[19]: 12
In [20]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment