Reputation: 4318
I am not very familiar with writing classes in python, I am trying to modify one written class for my application. the problem is that I want to give halo_pos
parameter as an input to the following NFW
class:
class NFW(object):
_req_params = { 'mass' : float , 'conc' : float , 'redshift' : float }
_opt_params = { 'halo_pos' :[float,float] , 'omega_m' : float , 'omega_lam' : float }
_single_params = []
_takes_rng = False
_takes_logger = False
def __init__(self, mass, conc, redshift, halo_pos,
omega_m=None, omega_lam=None, cosmo=None):
self.M = float(mass)
self.c = float(conc)
self.z = float(redshift)
self.halo_pos.x = float(halo_pos[0])
self.halo_pos.y = float(halo_pos[1])
self.cosmo = cosmo
if I pass the input for halo_pos
for the following parameters
>>> Xpos.value
array(235.0)
>>> type(Xpos.value)
<type 'numpy.ndarray'>
>>> Ypos.value
array(340.0)
omega_matter=0.23;omega_lambda=0.77
then when I try to call NFW class with the given inputs I get the following error message:
nfw = nfw_halo.NFWHalo(mass=M,conc=concentration,redshift=z_halo,halo_pos=[Xpos,Ypos],omega_m=omega_matter,omega_lam=omega_lambda)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "nfw_halo.py", line 156, in __init__
self.halo_pos.x = float(halo_pos[0])
AttributeError: 'NFWHalo' object has no attribute 'halo_pos'
How should I define halo_pos for my class in order to avoid raise error message?
Upvotes: 0
Views: 3585
Reputation: 140
You're not declaring the halo_pos variable that you're assigning in your init section.
class NFW(object):
_req_params = { 'mass' : float , 'conc' : float , 'redshift' : float }
_opt_params = { 'halo_pos' :[float,float] , 'omega_m' : float , 'omega_lam' : float }
_single_params = []
_takes_rng = False
_takes_logger = False
halo_pos = None
def __init__(self, mass, conc, redshift, halo_pos,
omega_m=None, omega_lam=None, cosmo=None):
self.M = float(mass)
self.c = float(conc)
self.z = float(redshift)
self.halo_pos = halo_positions(halo_pos)
self.cosmo = cosmo
print(str(self.halo_pos.x),str(self.halo_pos.y))
class halo_positions(object):
x = None
y = None
def __init__(self,positions):
self.x = positions[0]
self.y = positions[1]
if __name__ == '__main__':
nfw = NFW(mass=1.23,conc=2.34,redshift=3.45,halo_pos=[4.56,5.67],omega_m=1.111,omega_lam=1.222)
so your delclaration self.halo_pos.x and self.halo_pos.y are cleaned up once init finishes.
Upvotes: 1