1

Four Vectors

import numpy as np
class FourVector:
""" This document is a demonstration of how to create a class of Four vector """
    def __init__(self,ct=0,x=0,y=0,z=0):
        self.a=(ct,x,y,z)
        self.r=(ct,r=[x,y,z])

P0 = FourVector()
print P0.a

P1 = FourVector(ct=9,x=1,y=2,z=4)
print P1.a

P2 = FourVector(ct=99.9,r=[1,2,4])

My code is working fine for P0, P1 but doesn't work for P2 :( Can anyone spot my mistake?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
el psy Congroo
  • 354
  • 1
  • 4
  • 19

3 Answers3

2

r isn't even in the argument list, why? Just add it:

def __init__(self,ct=0,x=0,y=0,z=0, r=None)
aIKid
  • 26,968
  • 4
  • 39
  • 65
  • :) but OP probably doesn't yet :P – askewchan Nov 13 '13 at 02:37
  • No, i don't feel a need to mention it, since `__init__` is a special method that will be called only once, when the class is instantiated. – aIKid Nov 13 '13 at 03:06
  • 1
    @aIKid: Wrong. `__init__` will be called to instantiate each instance, and each instance will share the same default list for `r`. This kind of thing is why you don't use a mutable default argument, even if you think it should be safe. – user2357112 Nov 13 '13 at 03:31
2

You don't have a r parameter in your __init__ method:

class FourVector:
    def __init__(self, ct = 0, x = 0, y = 0, z = 0, r = None):
        self.a = (ct, x, y, z)
        if r is not None:
            self.a = (ct, r[0], r[1], r[2])

P0 = FourVector()
print P0.a

P1 = FourVector(ct = 9, x = 1, y = 2, z = 4)
print P1.a

P2 = FourVector(ct = 99.9, r = [1, 2, 4])
print P2.a
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • I was struggling to create another layer... " like what you did above, by stating r!=[] Flexibly using if statement, I'm a true newbie in programming, just need more practice! – el psy Congroo Nov 13 '13 at 02:22
  • `if r != []` means "if you passed a parameter `r` to `__init__`" – Christian Tapia Nov 13 '13 at 02:23
  • 2
    Note that this isn't a correct solution to the assignment. The space components are supposed to be stored in a numpy array. Also, it's a bad idea to have a mutable default argument, even if you don't happen to mutate it. That said, this solves the immediate bug. – user2357112 Nov 13 '13 at 02:24
  • Instead of `r = []` and `if r != []:`, I recommend `r = None` and `if r is not None:`. See [this post](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) for a detailed explanation. – SethMMorton Nov 13 '13 at 02:58
1
import numpy as np

class FourVector:
""" This document is a demonstration of how to create a class of Four vector """
    def __init__(self, ct=0, x=0, y=0, z=0, r=[]):
        self.ct = ct
        self.r =  np.array(r if r else [x,y,z])


P0 = FourVector()
print P0.r

P1 = FourVector(ct = 9, x = 1, y = 2, z = 4)
print P1.r

P2 = FourVector(ct = 99.9, r = [1, 2, 4])
print P2.r
trstowell
  • 33
  • 1
  • 1
  • 5
  • It's probably better to use `None` for the default argument `r`, so you don't accidentally modify the list and get weird bugs. – user2357112 Nov 13 '13 at 02:22
  • This is what @user2357112 is referring to: http://stackoverflow.com/q/1132941/1730674 – askewchan Nov 13 '13 at 02:33
  • \@askewchan Don't we avoid this issue since 'r' is defined on class initialization? I may be missing something, and all else equal, using r=None in the code I provided works just fine, and if there's a non-zero chance to create bugs due to something unknown to me, I'll start using None defaults. Thanks. – trstowell Nov 13 '13 at 20:11