Question

I'm a bit new to forcing python code to be PEP8, what is the preferred way to PEP8 this line of code:

emissionprob = preprocessing.normalize(self.random_state.rand(self.n_components, self.n_symbols), norm='l1', axis=1, copy=False)

personally I'm a bit weird and like it this way, is this considered PEP8?

emissionprob = preprocessing.normalize(
    self.random_state.rand(
        self.n_components, 
        self.n_symbols
    ), 
    norm='l1', 
    axis=1, 
    copy=False
)
Was it helpful?

Solution 2

I have the same preference as you do and the pep8 validator doesn't think it's wrong: https://pypi.python.org/pypi/pep8

Usually I think it's recommended to do it like this however, I find it less readable:

emissionprob = preprocessing.normalize(self.random_state.rand(self.n_components, 
                                                              self.n_symbols), 
                                       norm='l1', 
                                       axis=1, 
                                       copy=False)

I personally do it like this (note the trailing comma's everywhere):

emissionprob = preprocessing.normalize(
    self.random_state.rand(
        self.n_components, 
        self.n_symbols,
    ), 
    norm='l1', 
    axis=1, 
    copy=False,
)

OTHER TIPS

I'll just post how I would do it, I think this looks cleaner but as I said before once you obey the basics of PEP-8, the rest is just a matter of personal style:

emissionprob = preprocessing.normalize(
    self.random_state.rand(self.n_components, self.n_symbols),
    norm='l1',
    axis=1,
    copy=False)

IMHO you are using way too many extra lines, it just seems like you are going overboard with it.

I think the way you are doing it is fine, but I would try to keep it consistent with the existing code.

In this special case, I would probably assign the first argument to a temporary variable.

rand = self.random_state.rand(self.n_components, self.n_symbols)
emissionprob = preprocessing.normalize(rand, norm='l1', axis=1, copy=False)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top