joelgrus / data-science-from-scratch

code for Data Science From Scratch book
MIT License
8.63k stars 4.5k forks source link

pg 32 construction of the counting clicker #75

Closed smithjackson35 closed 5 years ago

smithjackson35 commented 5 years ago

I am relatively new to Python and am having issues building the counting clicker on page 31-32.

I start with the

class CountingClicker: """A class can/should have a docstring, just like a function""" then

def _init__(self, count = 0): self.count = count

then I try to initialize the clickers

clicker1 = CountingClicker() #this works fine clicker2 = CountingClicker(100) #this breaks

I get the error message that counting clicker takes no arguments.

I though at first I replaced "init" above with "CountingClicker" but that did not work either and results in the same error message.

Can someone please help I am struggling with learning classes and would really appreciate it. Let me know if I need to provide my actual .py file.

joelgrus commented 5 years ago

can you share your actual .py file?

joelgrus commented 5 years ago

or just paste the code for it in a github gist

smithjackson35 commented 5 years ago

(base) C:\Users\johndoe>ipython Python 3.7.3 (default, Apr 24 2019, 13:20:13) [MSC v.1915 32 bit (Intel)] Type 'copyright', 'credits' or 'license' for more information IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: conda activate dsfs

Note: you may need to restart the kernel to use updated packages.

In [2]: class CountingClicker: ...: """A class can/should have a docstring, just like a function""" ...:

In [3]: def init(self, count = 0): ...: self.count = count ...:

In [4]: clicker1 = CountingClicker() #initialized with 0

In [5]: clicker2 = CountingClicker(100) #starts with count 100

TypeError Traceback (most recent call last)

in ----> 1 clicker2 = CountingClicker(100) #starts with count 100 TypeError: CountingClicker() takes no arguments In [6]: In [6]: def __CountingClikcer__(self, count = 0): ...: self.count = count #checking if i need to actually replace __init__ with 'CountingClicker' ...: ...: In [7]: clicker1 = CountingClicker() In [8]: cliker2 = CountingClicker(100) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in ----> 1 cliker2 = CountingClicker(100) TypeError: CountingClicker() takes no arguments In [9]:
smithjackson35 commented 5 years ago

ClassCodeIssue_DSFS.txt

joelgrus commented 5 years ago

the problem is with the way you're using IPython console. Whenever you put in two blank lines, that tells it that you're done giving it code and that it should run what you've given it. So, for example, when you do:


In [2]: class CountingClicker:
   ...:     """A class can/should have a docstring, just like a function"""
   ...:

In [3]: def __init__(self, count = 0):
   ...:     self.count = count
   ...:

in the first you're defining an empty class and then saying "I'm done with this class", and in the second you're defining a bare function that (now) has nothing to do with the previous class.

to do this kind of thing in IPython console you need to not use blank lines:

In [1]: class CountingClicker: 
   ...:     def __init__(self, count=0): 
   ...:         self.count = count 
   ...:                                                                                                                         

In [2]: CountingClicker()                                                                                                       
Out[2]: <__main__.CountingClicker at 0x7f1264413ac8>

In [3]: CountingClicker(10)                                                                                                     
Out[3]: <__main__.CountingClicker at 0x7f12643c40b8>

or else use the %paste magic:

https://stackoverflow.com/questions/10886946/how-does-ipythons-magic-paste-work

smithjackson35 commented 5 years ago

Awesome! Thanks for your help.