3

I have a class to assign some parameters:

class body:
 def __init__(self, name, number, L):
 self.name = name
 self.number = number
 self.L = L

And I would like to assign these parameters to 10 almost equal bodies like:

for i in range(0, 10):
 body[i].name = "test_name"
 body[i].number = i
 body[i].L = 1.

And to be able to change, lets say, the parameter L of body 3 from 1 to 2:

body[3].L = 2

Thank you very much for the help.

Eric Wilson
59.8k82 gold badges208 silver badges272 bronze badges
asked May 8, 2013 at 22:51
0

1 Answer 1

12

Note that body is a class. Using body[i] suggests you may be intending to use body as a list. If you want to create a list of 10 instances of body, do not name the list body as well. You could instead name the list bodies and define it with a list comprehension:

bodies = [body("test_name", i, 1.) for i in range(0, 10)]
bodies[3].L = 2

By the way, PEP8 Style Guide recommends all classes follow the CapWords convention. So to conform with the convention, body should be Body. By following this convention, everyone reading your code will understand immediately what is a class and what is not.

answered May 8, 2013 at 22:52
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.