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?

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

I still don't understand using KC=KC ?
@Rahul: would it help if you added print(CC, PN, KC) to your function? You'll see what happens to the values.
@Rahul note that KC="" means different things when defining a function with default keyword arguments as opposed to calling a function with keyword arguments.
I know default thing but KC=KC was confusing. Now got it.
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.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.