Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

first of all sorry for this easy question. I have a class

class Grid(object):
    __slots__= ("xMin","yMax","nx","ny","xDist","yDist","ncell","IDtiles")
    def __init__(self,xMin,yMax,nx,ny,xDist,yDist):
        self.xMin = xMin
        self.yMax = yMax
        self.nx = nx
        self.ny = ny
        self.xDist = xDist
        self.yDist= yDist
        self.ncell = nx*ny
        self.IDtiles = [j for j in np.ndindex(ny, nx)]
        if not isinstance(nx, int) or not isinstance(ny, int):
            raise TypeError("an integer is required for nx and ny")

where

xMin = minimum x coordinate (left border)
yMax = maximum y coordinate (top border)
nx = number of columns
ny = number of rows
xDist and yDist = resolution (e.g., 1 by 1)

nx and ny need to be an integer. I wish to ask where is the most elegant position for the TypeError. Before all self. or in the end?

share|improve this question
Ideally, you should write your whole Grid class and then show it to us. Then we'd suggest all the ways in which it could be improved rather then asking lots of specific questions. – Winston Ewert Feb 21 at 18:40

1 Answer

class Grid(object):
    __slots__= ("xMin","yMax","nx","ny","xDist","yDist","ncell","IDtiles")
    def __init__(self,xMin,yMax,nx,ny,xDist,yDist):

Python convention says that argument should be lowercase_with_underscores

        self.xMin = xMin
        self.yMax = yMax

The same convention holds for your object attributes

        self.nx = nx
        self.ny = ny
        self.xDist = xDist
        self.yDist= yDist
        self.ncell = nx*ny
        self.IDtiles = [j for j in np.ndindex(ny, nx)]

This is the same as self.IDtiles = list(np.ndindex(ny,nx))

        if not isinstance(nx, int) or not isinstance(ny, int):
            raise TypeError("an integer is required for nx and ny")

This would be more conventialy put at the top. But in python we prefer duck typing, and don't check types. You shouldn't really be checking the types are correct. Just trust that the user of the class will pass the right type or something close enough.

share|improve this answer
Thanks @Winston Ewert. I didn't get about "duck typing". Do you suggest to avoid the "TypeError"? and the argument should be lowercase_with_underscores do you intend example self.__xMin = xMin? – Gianni Feb 21 at 18:45
1  
@Gianni, xMin should be x_min. There should be no capital letters in your names. Basically, only class names are named using capital letters. Re: duck typing. Just don't throw the TypeError. The python convention is to not check the types and just allow exceptions to be thrown when the type is used in an invalid manner. – Winston Ewert Feb 21 at 18:49
Thank Winston really useful post for me. I have just the last question to clarify my doubts. inside a class if i have a function def get_distance_to_origin(xPoint,yPoint): #do some stuff (es euclidean distance). Do i need to write x_point and y_point in order to respect the style? – Gianni Feb 21 at 18:58
@Gianni, yes, that is correct – Winston Ewert Feb 21 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.