0

I have been trying to fully understand this for a while now, and practically speaking I think I understand what happens but I can't seem to find anywhere that confirms wether I understood it correctly:

class test(object):
    def __init__(self, this):
        self.something = this
example = test("writing")

My question is: In the above example, is it correct that self is simply a stand-in for the instance I am creating? Meaning that when i create an instance and assign it to "example", then "example is put in place of self and behind the scenes does something resembling this:

class test(object):
    def __init__(example, this):
        example.something = this
example = test("writing")

Furthermore, does that also mean that as long as I am still working with this on a class basis (say in tandem with another class) I should still be using self.something, while I should be using example.something if I am working with it on an instance level?

I hope that made somewhat sense, im still trying to wrap my head properly around all of it, so let me know if I need to try and rephrase it.

For reference sake, should someone else end up asking the same, this reply: Python __init__ and self what do they do? almost did the trick for me, and only really left me a bit in doubt about the above questions.

Community
  • 1
  • 1
  • `self` is the object you are initialising, but it's not "standing in" for `example`. `example` doesn't get assigned to until _after_ the initialisation. – khelwood Jan 25 '17 at 16:04

2 Answers2

0

This is correct. self is the instance of the class (i.e. the object) and you use it inside the class code (inside it's methods).

While the first argument can be named something else (example in your second code), the convention is that we always use self or the code might be highly confusing for other programmers. But you got the gist right by doing that, the example variable in the class (i.e. the self in your first code) and the example variable outside of the class is basically the same thing.

By the way, I'd also avoid the following two things:

  • having a class name that starts with a small leter case,
  • using a variable name this (since a variable named this does in some other languages essentially what self does in Python).
Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
  • Thank you, and good to know in regards to using "this", would be quite the annoying habbit to have to struggle loosing it later on. –  Jan 25 '17 at 16:40
0

In Python, variables do not "contain" objects, they refer to them. So:

class test(object):
    def __init__(self, this):
        self.something = this

example = test("writing")

In this case example is a reference to the new object, but so is self. It is perfectly legal, and common, to have multiple references to the same object.

If you did:

another = example

this would not create a new object but have another reference to the same object. another, example (and self) would be references to the same single object.

You can test this by looking at the object's unique identifier, using id(). Add:

another = example

print id(another)
print id(example)

you will find that their id's are the same.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • Neat, gonna mess around with id() a bit. –  Jan 25 '17 at 16:41
  • @BlueNebulae: in the C implementation of Python the number is actually a virtual address (C pointer value) and demonstrates the principle quite well. – cdarke Jan 25 '17 at 18:18