Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set list value for attribute of class in python

I have created a class distance_neighbor in which one of the attributes is a list of objects of class Crime.
That is the value for all attributes I get from database query result.

At first, I have set data_Crime list as the value for attribute **Crime on class distance_neighbor, and I used del to clear data_Crime list after used, so that the data_Crime list can used in the next loop.

This is my code:

conn = psycopg2.connect("dbname='Chicago_crime' user='postgres'  host='localhost' password='1234'")
cur= conn.cursor()

minDistance=float(input("Nilai minimum distance : "))
cur.execute("""SELECT id_objek1, objek1, id_objek2, objek2, distance from tb_distance1 where distance<'%f'""" %(minDistance))

class Crime:
   def __init__(self, id_jenis, jenis):
      self.id_jenis=id_jenis
      self.jenis=jenis

class distance_neighbor (Crime):
   def __init__(self, distance, **Crime):
      self.distance = distance
      self.Crime = Crime

data_Crime =[]
data_distance = []

for id_objek1, objek1, id_objek2, objek2, distance in cur.fetchall():
    data_Crime.append(Crime(id_objek1,objek1))
    data_Crime.append(Crime(id_objek2,objek2))
    data_distance.append(distance_neighbor(distance, data_Crime))
    del data_Crime[:]

error Message:

data_distance.append(distance_neighbor(distance, data_Crime))   
TypeError: __init__() takes exactly 2 arguments (3 given)

I have fixed my code using below answers guys, Thank you

like image 890
Erna Piantari Avatar asked Apr 20 '26 01:04

Erna Piantari


1 Answers

This should be closer to what you want:

class Crime(object):
    def __init__(self, id_jenis, jenis):
        self.id_jenis=id_jenis
        self.jenis=jenis

class DistanceNeighbor(object):
    def __init__(self, distance, crimes):
        self.distance = distance
        self.crimes = crimes


data_distance = []
for id_objek1, objek1, id_objek2, objek2, distance in cur.fetchall():
    crimes = [Crime(id_objek1,objek1), Crime(id_objek2,objek2)]
    data_distance.append(DistanceNeighbor(distance, crimes))

Classes in Python 2 should always inherit from object. By convention, class names are in CamelCase.

The inheritance of DistanceNeighbor from Crime seems unnecessary. I changed this.

Attributes to instance should be lower case, therefore I used crimes instead of the very confusing reuse of the class name Crime.

This line:

def __init__(self, distance, **Crime):

takes your list of Crime instance apart as separate arguments. In your case it means the __init__ receives:

distance, data_Crime[0], data_Crime[0]

this causes this error message:

TypeError: init() takes exactly 2 arguments (3 given)

The instantiation of Crime is pretty short. So, instead of the two appends you can create the list of the two Crime instances in one line:

crimes = [Crime(id_objek1,objek1), Crime(id_objek2,objek2)]

Since this creates a new list in each loop, there is no need to delete the list content in each loop, as you did with del data_Crime[:].

like image 139
Mike Müller Avatar answered Apr 21 '26 17:04

Mike Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!