Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

tef posted:

RIP The old thread.

To open the new thread, here's something I wish I knew all those years ago:


code:
a_string = (
    "One"
    "Two"
    "Three"
)
Gives "'OneTwoThree"

Potentially a double-edged sword, because if I try to write a list of strings as in

code:
a_string = (
    "One",
    "Two",
    "Three",
)
and I screw up by omitting a comma somewhere in the middle, I could see unexpected results.

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Boris Galerkin posted:

(but mostly because I don't think it's semantically correct to call a function to return a value that I've already computed).

There are plenty of examples of situations where "calling a function to return a value already computed" is normal and accepted*, so I think you're going to have to accept that this viewpoint is highly idiosyncratic. Coming up with an attribute of your object that behaves in some cases like a function and in other cases like the return value of the same function is pretty far from idiomatic, so you're going to have to accept that if you do that, just about anyone who needs to read or understand your code is going to have trouble doing so. That's slightly dependent on the language background of the individual but I think it would apply for most languages in common use. If I see ob.f() in one place and then ob.f somewhere else, I'm naturally going to think that the use of ob.f is to refer to the method itself (as Eela6 says) rather than to invoke it; I'll then be surprised to learn that's not what's happening.

You should not in general see invocation of functions as an expense.

* Aside from concepts like getter methods, consider the case of an object with an attribute that's time-consuming to calculate. In this case, it wouldn't be unusual to cache the latest calculated value for the attribute and also have a flag variable that indicates whether the stored value is stale. You then would add a method to allow the user of the object to fetch the value of the attribute. If the "stale" flag is set then the attribute is computed and then the computed value is stored and returned; if it's not, then the stored value is returned as-is.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Boris Galerkin posted:

so honestly I have no clue what the point of objects are or when and why I should use them.

The point of an object is to package up the knowledge and responsibilities of parts of your program into little packages that are easy for human beings to understand and reason about. If you were programming a first-person shooter game*, you might have a "player" object, "enemy" objects, "projectile" objects and so forth. Then instead of keeping track of enemies' health in some array of health values or something, you can access an individual enemy's health through that enemy object. If the enemy takes damage, you call a method enemy.take_damage(amount), which would take care of things like checking whether the enemy is now dead, whether it should change its behaviour now that it has less health, and so on. A lot of the benefits of this are already realised through the use of functions, which is an even more fundamental way to make programs easier to understand, but there are additional benefits that come from strongly associating data (the enemy's health) with the code that needs to care about it (the method take_damage()). A lot of usages of classes are more abstract than that, but the key idea is always making it easier for people to reason about your code and dividing up the responsibilities of your program.

* not that I would choose to try to do that in Python, mind you.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply