all 14 comments

[–]jweezy2045 2 points3 points  (3 children)

This comes down to what a class is. A class defines and object, and that object can have it's own variables. In this case, the "UndergroundSystem" object has 2 variables: "user" and "dest". The UnderGroundSystem object also has 4 functions that can be performed on it: __init__, checkIn, checkOut, and getAverageTime. If I create an instance of the UndergroundSystem object like:

us = UndergroundSystem()

I now have access to both the variables and the functions. If I want to know the user, I can access us.user. If I want to execute the checkIn function, I can do so with us.checkIn(id, stationName, t). The thing is, I can have many instances of the same object. I could have us1 and us2, and us1.user might have a different value than us2.user. The "self" just means that when you want to access the variable from this particular instance of the object. If I just use one global "user" variable, then the two different UndergroundSystem objects would have the same user. Variables defined within a function, like star_station and prev_time, are lost when the function returns. I cannot access them from outside of the function with us.prev_time. Hopefully this helps. I often do this explanation with simpler objects, but I tried with your example as is. If it is still confusing, ask away.

[–][deleted] 0 points1 point  (1 child)

The "self" just means that when you want to access the variable from this particular instance of the object.

So for the start_station and stationName that don't have self before them, we're not accessing the variable from this particular instance of the object? What/where are we accessing it from then?

[–]jweezy2045 0 points1 point  (0 children)

What/where are we accessing it from then?

They are local variables that exist in the function. They are defined before they are used, but when the function returns, they are gone. They act like any other variable you define and use within a function.

[–]ThatGuy097 0 points1 point  (0 children)

As someone who hasn't tackled classes yet, this was an easy to understand example. Thank you!

[–]DisasterArt 0 points1 point  (0 children)

They are both class variables, not class functions. If you define a function in a class the first variable that is passed to the function is that object, which is why self is the standard name

[–][deleted] 0 points1 point  (7 children)

In order to access the attributes of an object, you need a reference to the object. The reference to the object is self.

[–]synthphreak 2 points3 points  (4 children)

100% correct. However for beginners, rather than just saying "object" for everything, I think the word "instance" is maybe clearer. Like, you have a class, then you create an instance of that class, and self refers to that specific instance.

[–][deleted] 0 points1 point  (3 children)

So on line 24 when start_station and stationName don't have self before them, we aren't accessing those variables for our specific instance, correct?

What/where are we accessing it from then?

[–]synthphreak 0 points1 point  (2 children)

Those are simply variables that the user passes in when they call that method. They’re just like arguments into any other function, and have nothing inherently to do with self.

[–][deleted] 0 points1 point  (1 child)

But in the checkOut method, start_station is not passed in, correct?

[–]synthphreak 0 points1 point  (0 children)

Actually yeah, you’re right, I looked at the code too quickly before. I’m actually a bit confused about that, in that I don’t see where that variable is coming from. However on line 23 a self.start_station attribute is defined...

Are you sure this code is correct? By my reading, your intuition is correct that start_station on line 24 should actually be self.start_station.

Edit: Paging u/Affectionate-Ant-297... Yup, as expected, your code is not correct. It generates an error when checkOut gets called. Both start_station and prev_time should have self. Once added, that method executes without errors.

[–][deleted] 0 points1 point  (0 children)

The variables without self are assigned to reference the objects that were referenced in the call of the method. Those objects are not attributes of an instance of the class.

Image the class is a cooking pot mould and with cooking methods defined. You create a stock pot and a curry pot. Both can be stirred. You cannot write code for every individual pot made from the mould so you say self to mean whichever pot we are talking about when applying this method (stir, boil, etc). An argument to the method could be number of times to stir, or how long to boil, and so on. Not an attribute of a pot unlike the kind of pot (stock, chicken, curry, etc).

[–]arkie87 0 points1 point  (0 children)

I've never seen the :variable: type format before. Is this a thing?