2

I'm a bit confused about actual object and its reference in python.Googled a lot but didn't get what I need.

Does object creation also returns a reference to that object? Why i'm asking this is because I'm able to use actual object with . delimiter to call its methods.We usually use reference of the object to call methods.An example might give a clear idea of what I'm asking.

l = [2,4,1,3] # 'l' is a reference to actual object.Right?

does the RHS part create the object and return a reference to that object which'll be assigned to LHS ?

Because I'm able to use both list.sort() and [2,4,1,3].sort() to sort the list. Is [2,4,1,3] an actual object or does it yield a reference along with creating an object?

Similar is the case with Classes.

obj=SomeClass() #obj is a reference to object created by SomeClass().

Does SomeClass() return a reference along with creating an object?

Because we can use both obj.method1() and SomeClass().method1 to call methods.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
tez
  • 4,990
  • 12
  • 47
  • 67
  • 3
    Perhaps a [previous answer](http://stackoverflow.com/a/12080644) of mine helps you understand this? – Martijn Pieters Aug 30 '12 at 09:02
  • 1
    The following link discusses this, specifically how Python differs from other languages: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables – Pascal Bugnion Aug 30 '12 at 09:10

1 Answers1

3

I suggest you reading The Python Language Reference "Data Model".

In python, when you create an object, it's being allocated in heap. If you assign your object to some variable, you actually are tagging it. One object may have many tags. When object looses all tags it may be destroyed by python garbage collector. So, an answer to your question -- [1,2,3] does not return any references, but simply creates new object.

EDIT:

Actually, during

[1, 2, 3].sort()

There is a reference to [1, 2, 3] list, that stays in VM's stack. But, as said before, it's not returned. But during:

l = [1, 2, 3]

This reference is copied from VM's stack to l.

Xentatt
  • 1,264
  • 22
  • 35
  • So [1,2,3] is object.if a=[2,1,3],'a' is reference to the object [2,1,3]. Can you relate the analogy a.sort() vs [2,1,3].sort() ? – tez Aug 30 '12 at 09:32
  • 1
    I've added some edits above. Both examples are the same, the difference comes in the way, we refer to the list: through *a* variable or by VM's itself, taking this reference from it's stack. – Xentatt Aug 30 '12 at 09:37
  • Its all making some sense now ,though i didn't grasp the whole thing.But very soon i'll. Thank you so much – tez Aug 30 '12 at 09:48
  • 1
    You're always welcome. May be it's difficult for you to *grasp* it, 'courze you've got used to some other languages (like Java) and their implementations of Object manipulation. BTW, now we've been talking about cpython realization, but there many other ones (jython and so on), and each one may have it's own vision to this concept. – Xentatt Aug 30 '12 at 09:51