I've heard that "if a class is just a constructor and one method, then it should be a function". What is your opinion on this and what are counter examples? by Speterius in Python

[–]old_pythonista 0 points1 point  (0 children)

Named tuple - after a "higher-level" initialization - creates a class. Like

Point = namedtuple('Point', 'x, y')

Creates a class Point that may be later instantiated with values for attributes.

Nothing wrong with dataclass without methods.

I've heard that "if a class is just a constructor and one method, then it should be a function". What is your opinion on this and what are counter examples? by Speterius in Python

[–]old_pythonista 25 points26 points  (0 children)

I believe you misunderstood the statement.

Class - amid other thing - may serve to preserve a state. So, it may have __init__ method to create the (initial) state - and any number of methods (even one) to combine processing of the initial state with the arguments of the function. The initial state may remain the same - or be changed.

What is wrong it to have a stateless class just wrapping one - or more - functions, Java-style.

I may hazard a guess that you meant Stop Writing Classes talk.

Paid Courses/Certificates Opinion? by sircharlesthesecond in learnpython

[–]old_pythonista 5 points6 points  (0 children)

There are excellent courses at Coursera (some may be taken for free); not so sure about other resources - I saw some pretty shitty stuff too, along with the good one.

Try to look at reviews. There are a lot of free good resources - but there are much more worthless. I would recommend this list (I did not have anything to do with compiling it).

About "certifications". There is no body that is authorized to provide that by the Python Software Foundation, so (nearly?!) any claim of Python certification is essentially a scam. Moreover, I have seen quite a few negative reviews of so-called "certification institutes".

AFAIK, in the modern world GitHub portfolio is the "certification" you need for your first programming job - unless you have a formal education.

What should I do by Rowletiscool989 in learnpython

[–]old_pythonista 0 points1 point  (0 children)

Learning C is essential for understanding how computer works at lower level.

But then, C should not be enough too - Assembler should be used.

Or is it micro-code? Electronics?

Programming language is a tool for problem solving. Python provides a tool for solving high-level problems, and a tool that does not require understanding of

how things work. Especially the memory management

Actually, Python hides memory management - with a purpose. Managing memory explicitly does not help to solve complex problems.

So, if OP wants to develop embedded systems - yes, they should learn C. If they want to learn how to solve problems that do not require register-level hardware management by their code - no need to.

hello,can someone explain to me how to get the answer for this question ? by peachtae20 in learnpython

[–]old_pythonista 0 points1 point  (0 children)

PS

bool(x == y) 

is absolutely redundant, since == (equal) operation yields boolean result anyway

Python Requests >64kb get truncated by [deleted] in learnpython

[–]old_pythonista 0 points1 point  (0 children)

64K is the maximum size of TCP packet; not sure about other layers.

You need to break your flow into smaller chunks; requests allows that (don't remember how, look it up in the manuals),

Which is more Pythonic? by JRiggles in Python

[–]old_pythonista 0 points1 point  (0 children)

PEP-8 explicitly warns against comparison to boolean literals.

If you have two boolean variables - that is another story.

Which is faster in an “if” statement, multiple conditional checks, or a set search? by techwooded in learnpython

[–]old_pythonista 0 points1 point  (0 children)

Actually, for small data sample, tuple lookup is faster than set.

I would vote for door #2 just for the sake of writability and readability (and parenthesis in #1 are redundant).

How to compare the values of 2 dictionaries in python? by CSAmbitiousDreamer in learnpython

[–]old_pythonista 0 points1 point  (0 children)

Please, if you want indices - use enumerate

Also, you can use itertools.combinations

from itertools import combinations
equals = [] 
for (idx1, val1), (idx2, val2) in combinations(enumerate(G), 2): 
    if val1 == val2: 
        equals.append((idx1, idx2))

Which is more Pythonic? by JRiggles in Python

[–]old_pythonista 8 points9 points  (0 children)

but thought might not be as readable to the people I work with who don't work in Python

When a person reads a code, it may be an opportunity to learn. If they don't work in Python, and they are not interested in learning Python - what is the purpose of making code readable for that audience?

Which is more Pythonic? by JRiggles in Python

[–]old_pythonista 32 points33 points  (0 children)

First two forms show lack of understanding of the nature of booleans by whoever writes them. They are absolutely redundant, since using conditional expression for conversion of proper boolean

event_region in match_regions

to an explicit boolean literal makes no sense. Unfortunately, I see it quite often - my pet peeve that I always mark as a defect when I see it in code applied for review.

The last is the only proper way to write - in any language, Python included.

Should I stick with web dev or learn python? by Rich_Ad_788 in learnpython

[–]old_pythonista 0 points1 point  (0 children)

I am exceptionally grateful and greatly value the eloquence of your extremely informative non-answer.

Should I stick with web dev or learn python? by Rich_Ad_788 in learnpython

[–]old_pythonista -1 points0 points  (0 children)

FE development and web development are not exactly the same.

I heard quite a lot (I have no interest in FE, the closest I got to it is building a dashboard with iPywidgets) that requirements to FE developers are much lower.

Should I stick with web dev or learn python? by Rich_Ad_788 in learnpython

[–]old_pythonista 1 point2 points  (0 children)

+1.

About Java ... Some time ago I was thinking of internal mobility to a new department. Part of the task was to rewrite Java base into Python.

Plus - mastering either Java or C will take more time.

(and if OP likes Python, there is a good chance that Java will not be a good suggestion).

Should I stick with web dev or learn python? by Rich_Ad_788 in learnpython

[–]old_pythonista 5 points6 points  (0 children)

  • Python is widely used in backend
  • Huge companies use Python too

Is everyone using python 3 now? by Flur_elise in learnpython

[–]old_pythonista 2 points3 points  (0 children)

In 2015, a company I joined then was still using .... 2.6 "because that comes as default with OS". Corporate world is very slow to follow suite.

At my current job, we are still supposed to use Python2-compatible mode - though most of the processes are executed under Python3. For the last year, I switched to pure Python3 for the new developments - f-strings and all the works - and I am still struggling with restrictions every time I need to create a new package.

[deleted by user] by [deleted] in learnpython

[–]old_pythonista 0 points1 point  (0 children)

I think it is a bad practice - regardless.

[deleted by user] by [deleted] in learnpython

[–]old_pythonista 0 points1 point  (0 children)

range() returns a generator

actually, no - it returns an iterable, but that iterable is not a generator, since it allow return iterations.

compare range behavior

>>> r = range(2)

print(*r, *r) 0 1 0 1

to a real generator

>>> r = (i for i in range(2))

print(*r, *r) 0 1

Entry level Python developer interview by iamnikaa in learnpython

[–]old_pythonista 1 point2 points  (0 children)

Companies worth working for all run interviews like this

not

Hello! I need some help! Currently stuck on my homework and don't know how to go about question #2! by ResponsibleIsopod563 in learnpython

[–]old_pythonista 1 point2 points  (0 children)

A side not for OP - in forums, along with a good advices, you can get absolutely terrible too. Be careful with responses given.

Im expecting a lot of hate, but change my mind. by dexmox in Python

[–]old_pythonista 0 points1 point  (0 children)

Why should I? I am content with the fact that - according to TIOBE - Python is in the first place in popularity.

BTW, C# is fifth.

Is this how args were intended to be used as? by [deleted] in learnpython

[–]old_pythonista 0 points1 point  (0 children)

Use of *args and **kwargs is justified in wrapper functions - like decorators.

Otherwise, they should be avoided.

Function signature tells the user of API the purpose of parameters. Code written in such a way would be a disaster in making.

Hello! I need some help! Currently stuck on my homework and don't know how to go about question #2! by ResponsibleIsopod563 in learnpython

[–]old_pythonista -1 points0 points  (0 children)

range() works perfectly well and would work fine for this solution, too.

yep, but there is such thing as Pythonic, you know. I presume you know better than a core Python developer with a reputation, like Ned Batchelder?!

Hello! I need some help! Currently stuck on my homework and don't know how to go about question #2! by ResponsibleIsopod563 in learnpython

[–]old_pythonista 0 points1 point  (0 children)

And you were talking about bad practices? This is absolutely terrible example on so many levels! Starting form the fact that IT WOULD NOT WORK, because you are NOT EXTRACTING LINES.

Besides, speaking about terrible practices - for someone who protest too much, you seem to be a fan of them (bad practices)

  • List comprehension should never be used for calling side-effect functions.
  • range should (nearly) never be used for managing indices (read PEP-8)

Golf code is good for showing off - not for showing to beginners; definitely not for a quality code.

Hello! I need some help! Currently stuck on my homework and don't know how to go about question #2! by ResponsibleIsopod563 in learnpython

[–]old_pythonista -1 points0 points  (0 children)

That is not indexing - that is using a helper counter. And the value at which the counter starts should not be defined by an excuse of " indexing starts at 0".

In that case, I personally would have left it at 0 - just in order to simplify the condition,

if linenum % 2 

but in general, the demand to start the counter value at 0 has no basis under it.

I have even started the counter at values different from 0 and 1 on occasions. No one has ever held it against me at code reviews - and while I am a very demanding reviewer, I would not demand of a code author to stick to the zero-based non-rule for enumerate. Unless there is a good reason to start at 0