1

I am learning OOP in python.

I am struggling why this is not working as I intended?

class Patent(object):
    """An object to hold patent information in Specific format"""
    def __init__(self, CC, PN, KC=""):
        self.cc = CC
        self.pn = PN
        self.kc = KC

class USPatent(Patent):
    """"Class for holding information of uspto patents in Specific format"""
    def __init__(self, CC, PN, KC=""):
        Patent.__init__(self, CC, PN, KC="")


pt1 = Patent("US", "20160243185", "A1")

pt2 = USPatent("US", "20160243185", "A1")

pt1.kc
Out[168]: 'A1'

pt2.kc
Out[169]: ''

What obvious mistake I am making so that I am not able to get kc in USPatent instance?

Rahul
  • 10,830
  • 4
  • 53
  • 88

3 Answers3

4

You are passing in an empty string:

Patent.__init__(self, CC, PN, KC="")

That calls the Patent.__init__() method setting KC to "", always.

Pass in whatever value of KC you received instead:

class USPatent(Patent):
    """"Class for holding information of uspto patents in Specific format"""
    def __init__(self, CC, PN, KC=""):
        Patent.__init__(self, CC, PN, KC=KC)

Within USPatent.__init__(), KC is just another variable, just like self, CC and PN. It is either set to "" already, or to whatever was passed in when you call USPatent(...) with arguments. You simply want to call the Patent.__init__() method passing on all the values you have.

You can drop the keyword argument syntax from the call too:

Patent.__init__(self, CC, PN, KC)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I still don't understand using KC=KC ? – Rahul Oct 17 '16 at 07:49
  • @Rahul: would it help if you added `print(CC, PN, KC)` to your function? You'll see what happens to the values. – Martijn Pieters Oct 17 '16 at 07:53
  • 1
    @Rahul note that `KC=""` means different things when [defining a function with default keyword arguments](https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions) as opposed to [calling a function with keyword arguments](https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments). – tutuDajuju Oct 17 '16 at 07:59
  • I know default thing but KC=KC was confusing. Now got it. – Rahul Oct 17 '16 at 08:02
1
class USPatent(Patent):
    """"Class for holding information of uspto patents in Specific format"""
    def __init__(self, CC, PN, KC=""):
        Patent.__init__(self, CC, PN, KC="")

Here you pass KCas "" by coding KC="", instead of KC=KC

To pass the inputted KC:

class USPatent(Patent):
    """"Class for holding information of uspto patents in Specific format"""
    def __init__(self, CC, PN, KC=""):
        Patent.__init__(self, CC, PN, KC)
Michel Touw
  • 626
  • 6
  • 15
1

The line

Patent.__init__(self, CC, PN, KC="")

Should be

Patent.__init__(self, CC, PN, KC)

The former sets the argument with the name "KC" to the value "" (the empty string) using the keyword-style argument syntax. What you want is pass the value of the variable KC instead.

dkasak
  • 2,651
  • 17
  • 26