I have to create a function, printObjects(attribute, option), which must print the objects based on a certain attribute. The attribute can take ints or strings as input. The option attribute is optional. How do I implement this for my class?
-
2"The option attribute is optional" what?!?! I hope this means that the 2nd parameter of printObjects, which is named 'option', is not mandatory. Otherwise i am very confused with the different uses of option/attribute. Assuming you meant what i hope, what is option for? and what does it default to?jon_darkstar– jon_darkstar2010-11-10 17:31:48 +00:00Commented Nov 10, 2010 at 17:31
-
print the objects based on a certain attribute what is this supposed to mean? :-|fortran– fortran2010-11-10 17:41:42 +00:00Commented Nov 10, 2010 at 17:41
Add a comment
|
1 Answer
I assume you want something like this, or perhaps it will be helpful at least:
def printObject(obj, attributes=()):
for a in attributes:
print getattr(obj, a)
5 Comments
Never use something mutable as default argument. (Unless you actually know how it works and want this behaviour; also, it won' resul tin any bugs in this example but it's better to point this out anyway) See stackoverflow.com/questions/1132941/…
dan_waterworth
In this case it's fine, 'attributes' isn't written to.
Trim
This was the right way to do it. Thanks. Sorry about my ambiguity in the original question, but this guy got what I meant :)
mkm
@delnan, you are right that the the mutable argument is dangerous. It was to make the code easy to read, but I can use a tuple to achieve the same effect
Björn Pollex
@Trim: If this answer is correct for you, then accept it (use the tick to the left of the answer).