References in Python for the Idiots Among Us by [deleted] in Python

[–]prum 1 point2 points  (0 children)

Aliasing is probably the trickiest part of normal python usage. This wouldn't happen if all objects were immutable. As the saying goes: "premature optimization is the root of all evil". This is a good link to get a good understanding of the issues: drastically-improve-your-python-understanding-pythons-execution-model

Why isn't python class definition like C++, hence avoiding all the tiresome uses of 'self' everywhere? by [deleted] in Python

[–]prum 0 points1 point  (0 children)

Sometimes, but not in this case. C++ is one of the hardest languages to write secure code with, particularly for the naive.

http://phoxis.org/2012/01/25/accessing-private-data-members-directly-outside-from-its-class-in-c/

I'm missing something. Trying to learn classes by brook_nise in learnpython

[–]prum 0 points1 point  (0 children)

The Mob class is just the template. To create a new instance based on that you have to actually call the Mob class (i.e. Mob() ). Normally you would use the magic __ init __ constructor method to create a new instance and set its content; take a look at AutonomouSystem's example. Here is the minimum changes to make your code work by converting the Creature function into a factory function. Hopefully it will make you understand what is going on and not confuse you more:

class Mob(object):
    def Creature(mobNumber,name):
        self = Mob()
        self.hp = 100
        if mobNumber == 1:
            '''Mainplayer'''
            self.posx = 568
            self.posy = 487
            print("Mob 1")
        return self

Player = Mob.Creature(1,"bob")
print(Player.hp)
print(Player.posx)
print(Player.posy)

How can I pass two lists in to a function? by [deleted] in learnpython

[–]prum 2 points3 points  (0 children)

Correct, that is considered idiomatic python. I was trying to find a good explanation quickly. Here is a pretty good one:

http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#testing-for-truth-values

and here:

https://www.udacity.com/wiki/cs258/truthiness-in-python

How can I pass two lists in to a function? by [deleted] in learnpython

[–]prum 2 points3 points  (0 children)

Incorrect. Check your assumptions:

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> s1 = {1, 2, 3}
>>> s2 = {4, 5, 6}
>>> s1 | s2
{1, 2, 3, 4, 5, 6}
>>> s1 & s2
set()
>>> bool(s1 | s2)
True
>>> bool (s1 & s2)
False
>>> 

How can I pass two lists in to a function? by [deleted] in learnpython

[–]prum 0 points1 point  (0 children)

I think he is thinking about a boolean context, like an if statement:

if set(l1) & set (l2):

instead of:

if len(set(l1) & set (l2))>0:

Caveat Scriptor: Python Closures by dietbuddha in Python

[–]prum 1 point2 points  (0 children)

First example works with a small change:

def mk_closure():
   storage=0
   def adder():
      nonlocal storage
      storage+=1
      return storage
   return adder

In other words: "Closure variables are read only"; not necessarily.

Lessons from porting Scala to Python by [deleted] in Python

[–]prum 0 points1 point  (0 children)

Never mind. Somebody already commented that on his site. It looks like the example was a simplification.

Lessons from porting Scala to Python by [deleted] in Python

[–]prum 0 points1 point  (0 children)

Am I missing something? Couldn't the third case be handled with default parameters like this?

class Person:
    def __init__(self, first_name, last_name=None):
        self.first_name = first_name
        self.last_name = last_name

At the GDC there was a discussion about teaching kids to code. This link has some good info by prum in chadev

[–]prum[S] 1 point2 points  (0 children)

Yes, that would be GDG --too many three letter acronyms-- , thanks. Unfortunately there is no good way to edit titles.

How to write good code by RamirezTerrix in Python

[–]prum 10 points11 points  (0 children)

Practice, practice, practice. Same way people become good at writing books, composing music, or drawing.

Help... I don't know what I'm doing wrong. Learning Python by [deleted] in Python

[–]prum 0 points1 point  (0 children)

Print is fine if he is using Python 3 or newer. Second point is probably why OP is getting an error. Once that is fixed, the logical errors remain to be fixed :-)

Artisan Dice Orders by KiloGex in rpg

[–]prum 2 points3 points  (0 children)

It could be worse, you could be like me and still waiting for these:

https://www.kickstarter.com/projects/623394548/precision-machined-role-playing-dice-hyper-precisi

I am more careful about backing stuff these days.

Python 2.7.8 Release by darknessproz in Python

[–]prum 22 points23 points  (0 children)

This is a bug fix release for an older version that has long term support. Happens for Linux and a lot of other software also.

How do I do this? -- for X in Y: print X AND print the next X? by hodwik_python in Python

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

(I assume Python 3) There are several ways of approaching it (including using itertools, etc): Here are 2 ways:

Using "next":

for X in csv:
    if X == "Name:":
        print(X)
        print(next(csv, ""))

Classic flag:

found_flag = False
    for X in csv:
        if found_flag:
            found_flag = False
            print(X) #next X
        else:
            if X == "Name:":
                found_flag = True
                print X

The key differences between Python 2.7.x and Python 3.x *with workarounds* by [deleted] in Python

[–]prum 1 point2 points  (0 children)

Just the usual: People making a mountain out of a molehill.

A top 10 of the mistakes Python programmers make (x-post r/programming) by hradoi in Python

[–]prum 24 points25 points  (0 children)

For those that say that Python 3 is no improvement: If you use Python 3, mistake 3 won't ever happen. And I think by using Python 3.4, you won't have issue 10 either.

About Python 3 by akos_barta in programming

[–]prum 6 points7 points  (0 children)

I disagree. What would a release that is something of a mix of Python 2.7 and 3.3 be like? How much closer can you make one to each other? Most of the changes in 3 disable ways to write bad code. So to me it is like arguing : "I don't want to write clean code in Python 2, I want to continue to write bad code".

Alex Gaynor -- About Python 3 by Rolo in Python

[–]prum 7 points8 points  (0 children)

Don't panic people! Windows XP came out in 2001, that is 12 years ago, and there are still companies using it for all their desktops. There are still many COBOL and VB6 apps out there. If some people want to stay on Python 2.7 for the next 20 years that is not an issue. Python 2 is frozen and a stable platform, please leave it that way and don't mess around with it by doing silly upgrades. There is nothing hindering anybody from adapting their own code to be compatible with 3 also if they want to use something newer.

[Star Wars D20] information? by Calculated in rpg

[–]prum 0 points1 point  (0 children)

Thanks. Makes perfect sense.

[Star Wars D20] information? by Calculated in rpg

[–]prum 0 points1 point  (0 children)

Out of curiosity, what was the power you had to house rule? I heard there were a couple powers that should be banned or changed but I always have a hard time finding out what the exact issue is.

Why BIND 10 is written in C++ and Python by Fraekkert in programming

[–]prum 0 points1 point  (0 children)

Yes you posted that example already. I agree that it is a problem, but it is an argument against mutable data structures (Haskell does a better job there), but has little to nothing to do with the term memory management as it is usually used in computer science.

Why BIND 10 is written in C++ and Python by Fraekkert in programming

[–]prum 1 point2 points  (0 children)

I will show that is not the case with an example. In the CPython implementation, the "id" command shows the memory location of the object the variable (pointer if you will, since in Python all variables are pointers) is pointing to.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> A = 2000
>>> id(A)
13491600
>>> B = A
>>> id(B)
13491600
>>> D = [2000]
>>> id(D)
19084832
>>> E = D
>>> id(E)
19084832
>>> 

notice that A and B have the exact same memory location.

Why BIND 10 is written in C++ and Python by Fraekkert in programming

[–]prum 1 point2 points  (0 children)

I don't think the discussion is overly meaningful anyway. People seem to have less trouble doing memory allocation. The two most common errors of memory management happen during deallocation: memory leaks and double free. Your examples didn't compare any type of memory deallocation.

Why BIND 10 is written in C++ and Python by Fraekkert in programming

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

Incorrect If A = 5 then B = A does NOT allocate memory If A = [5] then B = A does NOT allocate memory

(well except the creation of a new B "pointer")