python-ecology-lesson icon indicating copy to clipboard operation
python-ecology-lesson copied to clipboard

Bug BBQ: Explanation of methods and attributes could be better

Open colinmorris opened this issue 7 years ago • 2 comments

Methods are first explained in lesson 00:

To add elements to the end of a list, we can use the append method. Methods are a way to interact with an object (a list, for example). We can invoke a method using the dot . followed by the method name and a list of arguments in parentheses. Let’s look at an example using append:

And then re-explained (along with attributes) in lesson 01:

There are many ways to summarize and access the data stored in DataFrames, using attributes and methods provided by the DataFrame object.

To access an attribute, use the DataFrame object name followed by the attribute name df_object.attribute. Using the DataFrame surveys_df and attribute columns, an index of all the column names in the DataFrame can be accessed with surveys_df.columns.

Methods are called in a similar fashion using the syntax df_object.method(). As an example, surveys_df.head() gets the first few rows in the DataFrame surveys_df using the head() method. With a method, we can supply extra information in the parens to control behaviour.

I think it would be helpful to make the following points:

  1. A method is just a kind of function.
  2. An attribute is just a kind of variable.
  3. Methods and attributes are specific to certain kinds of things. I can call append on a list, but not on a dictionary or a string. dtypes is not a general Python thing I can use anywhere like the type function - it's an attribute defined by Pandas DataFrames, so it only works on DataFrames.

Also, after the append example quoted above, there are a couple other places in the first lesson where methods are used (the items() and keys() methods of dictionaries). I feel like those are currently swept by way too fast. I think it's worth calling out what's going on more explicitly here. i.e. hey, look, more methods. Dictionaries have methods just like lists! What does this keys() method do? Let's check with the help function...

colinmorris avatar Nov 06 '17 01:11 colinmorris

Adding this to the list of issues for the Bug BBQ. Action items:

  • [ ] Better explanation of what a function is

  • [ ] Better explanation of what a method is

  • [ ] How are methods specific to types of objects? Perhaps include an example, and a discussion how you can view methods in the iPython/Jupyter framework (i.e., hitting tab).

wrightaprilm avatar Apr 05 '18 18:04 wrightaprilm

Please see pull-request #260 with rewritten intro-to-python which contains a more structured intro to functions and methods

katrintirok avatar Apr 08 '18 17:04 katrintirok

Fixed in #452

btovar avatar May 19 '23 12:05 btovar