@staticmethod or def function()?

2018-10-31 Thread Tobiah

My IDE (pycharm) suggests that I mark my class methods
with @staticmethod when they don't use 'self'.  Sounds
good.  I did that then it suggested I had the option
to make a regular function instead, at the file level.
That's a possibility.  I was thinking that I'd leave
the method in the class unless it was ever also used
by another class.  What do you think?



--
https://mail.python.org/mailman/listinfo/python-list


Re: customer compare in assertEqual

2018-10-31 Thread Stone Zhong
On Monday, October 29, 2018 at 5:29:11 PM UTC-7, Stone Zhong wrote:
> Hi There,
> 
> Now I want to make sure my code calls a function foo with an object t, 
> however, foo.assert_called_once_with(t) does not work, since t is a model 
> object and the code may load a different copy of t, so what I really want to 
> test is "It calls foo with t where t.id equals real_t.id, is there a way to 
> do that in python unit test?
> 
> Thanks,
> Stone

Thanks Peter, yeah, redefine the __eq__ works well.

- Stone

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: @staticmethod or def function()?

2018-10-31 Thread Santiago Basulto
I usually recommend my students to use @classmethod. It's a consistent API
with a regular instance method, and receiving the class as first parameter
(usually `cls`) is useful if you have to extend your classes.

About separating a staticmethod in a regular function, it's mostly a matter
of taste and organization. It's hard to say. Your intuition is good though,
if you need to use the functionality by several classes, then it's probably
not a staticmethod but an external function.

On Wed, Oct 31, 2018 at 12:58 PM Tobiah  wrote:

> My IDE (pycharm) suggests that I mark my class methods
> with @staticmethod when they don't use 'self'.  Sounds
> good.  I did that then it suggested I had the option
> to make a regular function instead, at the file level.
> That's a possibility.  I was thinking that I'd leave
> the method in the class unless it was ever also used
> by another class.  What do you think?
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Santiago Basulto.-
Up!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: @staticmethod or def function()?

2018-10-31 Thread Peter Otten
Tobiah wrote:

> My IDE (pycharm) suggests that I mark my class methods
> with @staticmethod when they don't use 'self'.  Sounds
> good.  I did that then it suggested I had the option
> to make a regular function instead, at the file level.
> That's a possibility.  I was thinking that I'd leave
> the method in the class unless it was ever also used
> by another class.  What do you think?

What do *you* think? Is your code demonstrably better if you turn the 
"normal" method into a static method?

Do not make changes to your code only to apeace a linter.

Regarding more the general question, should I use an instance method, a 
class method, a static method, or a function? -- that is hard to answer 
without an idea what the specific task of the function/method is, and how 
strong the link to the class is.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: @staticmethod or def function()?

2018-10-31 Thread Rob Gaddi

On 10/31/18 8:53 AM, Tobiah wrote:

My IDE (pycharm) suggests that I mark my class methods
with @staticmethod when they don't use 'self'.  Sounds
good.  I did that then it suggested I had the option
to make a regular function instead, at the file level.
That's a possibility.  I was thinking that I'd leave
the method in the class unless it was ever also used
by another class.  What do you think?





It should be a staticmethod if the code is, while not linked to any 
instance in particular, still fundamentally inherent to the class itself 
and of little use outside the context of that class.  It should be a 
stand-alone function if it provides stand-alone functionality.  The 
decision is also almost certainly not worth the amount of thought you're 
giving it.


--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


Re: @staticmethod or def function()?

2018-10-31 Thread Marko Rauhamaa
Peter Otten <__pete...@web.de>:

> Do not make changes to your code only to apeace a linter.

Precisely. Don't let a tool drive a show.

> Regarding more the general question, should I use an instance method,
> a class method, a static method, or a function? -- that is hard to
> answer without an idea what the specific task of the function/method
> is, and how strong the link to the class is.

I just always use regular instance methods. If I really want to
"disappear" self, I'll just take the function out of the class.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


lint appeasement (was: @staticmethod or def function()?)

2018-10-31 Thread Cameron Simpson

On 31Oct2018 21:23, Marko Rauhamaa  wrote:

Peter Otten <__pete...@web.de>:

Do not make changes to your code only to apeace a linter.


Precisely. Don't let a tool drive a show.


While this is sound for semantic changes (and a function versus a 
@staticmethod is verging on that), I would point out that cleaning lint 
is often useful in order to make the linter useful.


If the useful lint warnings are buried in a sea of dross, and you want 
to lint things, then it can be well worth making some changes to clean 
up noise.


Ideally of course the linter should recognise some hooks (typically 
special comments) to mark particular pieces of code as deliberately not 
matching the linter's opinions. And certainly I run some linters with 
various checks tuned or disabled.


I'm doing this on an ongoing basis in my libraries as I work on other 
changes: make the change, debug, then lint, with the linting in distinct 
commits unless it is tiny. This is gardually bringing consistency to the 
code and catching the odd bug or rough edge such as "variable set but 
not used".


The end game here is that your lint output should be empty so that when 
it isn't empty the messages are useful.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list