0

Some uncallable attributes aren't referencing existing objects but create objects when accessed. I'd like to know what the formal Python term for this is and how it's implemented.

An example is the T attribute of numpy arrays. It's equivalent to the transpose method with default arguments.

x = np.reshape(range(6), newshape = (2,3))
print(x)
# array([[0, 1, 2],
#        [3, 4, 5]])

print(x.T)
# array([[0, 3],
#        [1, 4],
#        [2, 5]])

print(x.transpose())
# array([[0, 3],
#        [1, 4],
#        [2, 5]])

print(x.T is x.T)
# False because x.T isn't referencing an existing array
BatWannaBe
  • 4,330
  • 1
  • 14
  • 23
  • 2
    Sounds like `x.T` is a _property_. – John Gordon Aug 03 '19 at 03:02
  • @JohnGordon: I don't think so. I've tried to reproduce that with a quick class and it works fine. – logicOnAbstractions Aug 03 '19 at 03:31
  • @StephenRauch it's great for you if you understand it, but if the point of SO was that if anyone understands a thing no one should ask it, then we wouldn't need SO. – logicOnAbstractions Aug 03 '19 at 03:34
  • You're asking "why is it worth a question", and seem to imply the demand is so trivial as to not be worth asking. Maybe you do understand what's happening here, but the OP doesn't. It seems like a perfectly fine a question and simply do not see the point in belittling it... – logicOnAbstractions Aug 03 '19 at 03:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197415/discussion-between-francky-v-and-stephen-rauch). – logicOnAbstractions Aug 03 '19 at 03:46
  • Interestingly, id(x) isn't equal to id(x.T). We'd expect that. But then do id(x.T) as many times as you want, you'll always get the same address. So I would have thought that is should return True - as the object is indeed (well according to id()) the same. – logicOnAbstractions Aug 03 '19 at 03:51
  • See for example https://stackoverflow.com/questions/6193556/how-do-python-properties-work – Karl Knechtel Aug 03 '19 at 03:56
  • 1
    The concept you are looking for is descriptors: https://docs.python.org/3/howto/descriptor.html – juanpa.arrivillaga Aug 03 '19 at 04:13
  • suddenly super() objects as class attributes and instance/class methods make a whole lot more sense – BatWannaBe Aug 03 '19 at 05:17

0 Answers0