Skip to main content
edited body
Source Link
9000
  • 24.4k
  • 4
  • 53
  • 80

1. Make your code easy to understand

Code is read much more often than written. Make your code maintainer's task easier (it as well may be yourself next year).

I don't know about any hard rules, but I prefer to have any future instance state clearly declared outright. Crashing with an AttributeError is bad enough. Not seeing clearly the lifecycle of an instance attribute is worse. The amount of mental gymnastic required to restore possible call sequences that lead to the attribute being assigned can easily become non-trivial, leading to errors.

So I usually not only define everything in constructor, but also strive to keep the number of mutable attributes to a minimum.

2. Don't mix class-level and instance-level members

Anything you define right inside the class declaration belongs to the class and is shared by all instances of the class. E.g. when you define a function inside a class, it becomes a method which is the same for all instances. Same applies to data members. This is totally unlike instance attributes you usually define in __init__.

Class-level data members are most useful as constants:

class Missile(object): MAX_SPEED = 100 # all missiles accelerate up to this speed ACCELERATION = 5 # rate of acceleration per game frame def move(self): self.speed += self.ACCELERATION if self.speed > self.MAX_SPEED: self.speed >= self.MAX_SPEED # ... 

1. Make your code easy to understand

Code is read much more often than written. Make your code maintainer's task easier (it as well may be yourself next year).

I don't know about any hard rules, but I prefer to have any future instance state clearly declared outright. Crashing with an AttributeError is bad enough. Not seeing clearly the lifecycle of an instance attribute is worse. The amount of mental gymnastic required to restore possible call sequences that lead to the attribute being assigned can easily become non-trivial, leading to errors.

So I usually not only define everything in constructor, but also strive to keep the number of mutable attributes to a minimum.

2. Don't mix class-level and instance-level members

Anything you define right inside the class declaration belongs to the class and is shared by all instances of the class. E.g. when you define a function inside a class, it becomes a method which is the same for all instances. Same applies to data members.

Class-level data members are most useful as constants:

class Missile(object): MAX_SPEED = 100 # all missiles accelerate up to this speed ACCELERATION = 5 # rate of acceleration per game frame def move(self): self.speed += self.ACCELERATION if self.speed > self.MAX_SPEED: self.speed > self.MAX_SPEED # ... 

1. Make your code easy to understand

Code is read much more often than written. Make your code maintainer's task easier (it as well may be yourself next year).

I don't know about any hard rules, but I prefer to have any future instance state clearly declared outright. Crashing with an AttributeError is bad enough. Not seeing clearly the lifecycle of an instance attribute is worse. The amount of mental gymnastic required to restore possible call sequences that lead to the attribute being assigned can easily become non-trivial, leading to errors.

So I usually not only define everything in constructor, but also strive to keep the number of mutable attributes to a minimum.

2. Don't mix class-level and instance-level members

Anything you define right inside the class declaration belongs to the class and is shared by all instances of the class. E.g. when you define a function inside a class, it becomes a method which is the same for all instances. Same applies to data members. This is totally unlike instance attributes you usually define in __init__.

Class-level data members are most useful as constants:

class Missile(object): MAX_SPEED = 100 # all missiles accelerate up to this speed ACCELERATION = 5 # rate of acceleration per game frame def move(self): self.speed += self.ACCELERATION if self.speed > self.MAX_SPEED: self.speed = self.MAX_SPEED # ... 
Source Link
9000
  • 24.4k
  • 4
  • 53
  • 80

1. Make your code easy to understand

Code is read much more often than written. Make your code maintainer's task easier (it as well may be yourself next year).

I don't know about any hard rules, but I prefer to have any future instance state clearly declared outright. Crashing with an AttributeError is bad enough. Not seeing clearly the lifecycle of an instance attribute is worse. The amount of mental gymnastic required to restore possible call sequences that lead to the attribute being assigned can easily become non-trivial, leading to errors.

So I usually not only define everything in constructor, but also strive to keep the number of mutable attributes to a minimum.

2. Don't mix class-level and instance-level members

Anything you define right inside the class declaration belongs to the class and is shared by all instances of the class. E.g. when you define a function inside a class, it becomes a method which is the same for all instances. Same applies to data members.

Class-level data members are most useful as constants:

class Missile(object): MAX_SPEED = 100 # all missiles accelerate up to this speed ACCELERATION = 5 # rate of acceleration per game frame def move(self): self.speed += self.ACCELERATION if self.speed > self.MAX_SPEED: self.speed > self.MAX_SPEED # ...