I am trying to convert the following Java code snippet into Python. Can someone please help me out with this. I am new to Python.
Java Code:
public class Person
{
public static Random r = new Random();
public static final int numFriends = 5;
public static final int numPeople = 100000;
public static final double probInfection = 0.3;
public static int numInfected = 0;
/* Multiple experiments will be conducted the average used to
compute the expected percentage of people who are infected. */
private static final int numExperiments = 1000;
/* friends of this Person object */
private Person[] friend = new Person[numFriends]; ----- NEED TO REPLICATE THIS IN PYTHON
private boolean infected;
private int id;
I am trying to replicate the same idea in the marked line above into Python. Can someone please convert “private Person[] friend = new Person[numFriends];” implementation into python. I am looking for a code snippet…thanks
Seems to me you want to know, what the equivalent of a fixed length array in Python is. There is no such thing. You don’t have to and you can’t pre-allocate memory like that. Instead, just use an empty list object.
Then use it like this:
It works essentially like a List< T > in Java.
Oh, and there are no access modifiers (private, public etc) in Python. Everything is public, so to say.