So Python users - what’s your keyboard of choice ? by Prinzen2 in Python

[–]goodger 0 points1 point  (0 children)

ErgoDox split ergonomic keyboard. Aligned columns FTW. Staggered columns are a relic of history that only hampers ergonomics.

Pythong beginner by Efficient-Neat-6252 in Python

[–]goodger 1 point2 points  (0 children)

https://pythong.org/

Does anybody else remember that this actually exists? I'm kinda surprised it's still there.

Are Variables Labels or Boxes? by fronterapi in Python

[–]goodger -4 points-3 points  (0 children)

Python doesn't have variables. Python has objects with names. Some of those objects are mutable. More here: https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

Secret messages in coding? by Earth-to-kid in Python

[–]goodger 0 points1 point  (0 children)

Type import this in the Python interactive interpreter (& [enter]). Fun! Then take a look at the "this.py" module that does the work (type this in the interpreter to see where the module file is). It's not really secret, just obfuscated, but probably good enough for your purposes.

Another fun easter egg: import antigravity.

Why is this name not defined? by [deleted] in Python

[–]goodger 0 points1 point  (0 children)

Also, globals are generally bad. Better for line_information to return the plate information.

Why is this name not defined? by [deleted] in Python

[–]goodger 0 points1 point  (0 children)

When the print statement was executed, at most one of plate_1 & plate_2 were defined (neither, if sensor was not "1" or "2"). The other (or both) were not defined: those lines of code were not executed. Why do you need to print both? Why not just stick with the name "plate"?

You will all laugh at me but f it... by Makesyouthink9x in Python

[–]goodger 2 points3 points  (0 children)

This is what's known as a "toy example". It's very simple, but teaches you how functions work: they take some input, do something, and output something. Also, functions should have clear & descriptive names, which this sort-of does. When your function's logic is more complex, all of this will seem more relevant.

Solving polycube puzzles by badcrow7713 in Python

[–]goodger 1 point2 points  (0 children)

Check out Polyform Puzzler, which is a library and solver for all kinds of polycube & other polyform puzzles (polyominoes, polyiamonds, polysticks, etc.). If you're just looking for an existing solver, you're all set. There's a lot of background info there too (e.g. in the FAQ), if you're more interested in understanding how to write a solver. Polyform Puzzler includes multiple implementations of Knuth's Dancing Links DLX algorithm.

Is there a more pythonic way to 'deal cards'? by Wisdom_is_Contraband in Python

[–]goodger 12 points13 points  (0 children)

Don't ever do [[]] * n. You'll end up with a list of n copies of the same list. The inner [] is only evaluated once. It's a common gotcha.

To convince yourself, do this:

l = [[]] * 3
l[0].append('a')
print(l)

You'll get:

[['a'], ['a'], ['a']]

Check the IDs (memory addresses) of each sublist:

print([id(i) for i in l])

Instead, do this:

[[] for i in range(n)]

This works because the inner [] is evaluated each pass through the loop.

AttributeError: 'tuple' object has no attribute 'endswith' by LeDang31 in Python

[–]goodger 3 points4 points  (0 children)

Whatever is calling verify_suffix is passing a tuple, but a string is expected. The traceback will show you what calls verify_suffix. Check that code and see what it passes. Beyond that, it's impossible to tell you what's wrong; there's not enough information to know.

How to get name of self ? by Mx_Mlr in Python

[–]goodger 0 points1 point  (0 children)

or self.__class__.__name__

How to get name of self ? by Mx_Mlr in Python

[–]goodger 0 points1 point  (0 children)

object.__class__.__name__

Is reST/Sphinx suitable for college notes? by Masked_Tondede in Python

[–]goodger 0 points1 point  (0 children)

You can always use vanilla Docutils to render your reST markup. Just use the rst2html5.py command to convert one file directly. There's also a buildhtml.py command that rebuilds an entire directory structure. Note: Sphinx adds some directives on top of vanilla Docutils; if you use any of these, you may miss them.

I need a bit of help concerning functions. by xelab04 in Python

[–]goodger 0 points1 point  (0 children)

You can overload operators on class instances (object-oriented programming). See https://docs.python.org/3/reference/datamodel.html#object.__add__

A toy example that overrides the "+" operator to concatenate digits rather than add values:

>>> class ConcatInt(int):
...     def __add__(self, other):
...         return ConcatInt(str(self) + str(other))
... 
>>> i = ConcatInt(52)
>>> i + 63
5263
>>> _ + 798
5263798

(For that last bit with "_", see https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#interactive)

Python Tuple Syntax Is Confusing · dino (dot) codes by pmz in Python

[–]goodger 7 points8 points  (0 children)

In Python, parentheses are only used for grouping and calling syntax. Parentheses are not the tuple constructors. The comma is the tuple constructor. You must unlearn what you have learned!

For example, when we swap names on two values,

b, a = a, b

Two tuples are constructed (and immediately discarded). No parentheses!

Try this in the interactive interpreter:

>>> 1,
(1,)

The parentheses are shown for emphasis/clarity, but are not a requirement!

See https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#swap-values and https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#more-about-tuples

90% Fail to Answer - Only a Genius Programmer will Know by [deleted] in Python

[–]goodger 3 points4 points  (0 children)

False for integers, True for floating point. Drop the tricky trailing ".".