Re: I want to lirn pithon but pithon doesn't want me to do so

@51
Yes but you really, really shouldn't.  In practice, if things aren't supposed to be going to certain places and you have to force them like that, it's a bug.

The problem with properties is that it's not obvious that there is anything magical going on.  If you are going to set a property to a specific value, and then when you read it back you're going to get a different value, then you should probably not use a property.  There is this idea called the principle of least surprise, which basically says that if you can describe part of your code as surprising then you probably shouldn't do it.  And if:

x = -2
foo.something = x
y = foo.something

ends such that y isn't equal to x, that's very surprising indeed.

So I'd either solve the underlying bug that lets things go negative when they shouldn't, or I'd do it via set_x and set_y with docstrings, like this:

class Player:
    # Lots of stuff....
    def set_x(self, x):
        """If x < 0, set x to 0, otherwise use x as given"""
        self._x = max(x, 0)

etc.  This doesn't violate the principle of least surprise, because you went through the trouble to call a method everywhere, and methods are expected to do/check things.

There are a very few good use cases for properties, but by default when you're going "How about I just use a property?" the answer is probably no.  Synthizer is using them, sort of, if you want a good example (the code doesn't make it look like this is the case, but it is).  In Synthizer it's useful to be able to set the position, and also the position you get back after setting the position is always the same as what you set, so it doesn't violate the principle of least surprise.  But that's not usually the case when doing the kinds of things that you're going to think of properties as being for, as someone who's still learning.



-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Meatbag via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector

Reply via email to