MCPcopy Create free account
hub / github.com/ipython/ipython / __getattr__

Method __getattr__

IPython/utils/ipstruct.py:125–151  ·  view source on GitHub ↗

Get an attr by calling :meth:`dict.__getitem__`. Like :meth:`__setattr__`, this method converts :exc:`KeyError` to :exc:`AttributeError`. Examples -------- >>> s = Struct(a=10) >>> s.a 10 >>> type(s.get) <... 'builtin_functio

(self, key)

Source from the content-addressed store, hash-verified

123 raise AttributeError(e)
124
125 def __getattr__(self, key):
126 """Get an attr by calling :meth:`dict.__getitem__`.
127
128 Like :meth:`__setattr__`, this method converts :exc:`KeyError` to
129 :exc:`AttributeError`.
130
131 Examples
132 --------
133
134 >>> s = Struct(a=10)
135 >>> s.a
136 10
137 >>> type(s.get)
138 <... 'builtin_function_or_method'>
139 >>> try:
140 ... s.b
141 ... except AttributeError:
142 ... print("I don't have that key")
143 ...
144 I don&#x27;t have that key
145 """
146 try:
147 result = self[key]
148 except KeyError:
149 raise AttributeError(key)
150 else:
151 return result
152
153 def __iadd__(self, other):
154 """s += s2 is a shorthand for s.merge(s2).

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected