Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

OOP - Giving a hand: Calling an argument as a function

On the Giving a Hand video, an argument to a class is called as a function in the for loop at the bottom here:

class Hand(list):
  def __init__(self, size=0, die_class=None,*args, **kwargs):
    if not die_class:
      raise ValueError("You must provide a die class")
    super().__init__()

    for _ in  range(size):
      self.append(die_class())

Can someone explain the syntax here and how a parameter gets called as a function?

[MOD: added ```python formatting -cf]

1 Answer

Steven Parker
Steven Parker
243,656 Points

To call a method, just add parentheses after the name.

So if the argument was named "sample", your code would be: "sample()" (assuming it did not require any arguments itself).

To enable more specific answers, always include a link to the course page you are working with.

Also, when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: