tags:

views:

383

answers:

4

Hi, this is my first question on StackOverflow, so please tell me how I can improve it in the comments.

I am writing a program to find adapters, and have made a class called 'Adapter'. When I pass in two arguments IDLE gives me an error saying I passed in three! Here is the code and stack trace:

#This is the adapter class for the adapter finder script

class Adapter:
    side1 = (None,None)
    side2 = (None,None)
    '''The class that holds both sides of the adapter'''
    def __init__((pType1,pMF1),(pType2,pMF2)):
        '''Initiate the adapter.

        Keyword Arguments:
        pType1 -- The passed type of one side of the adapter. ex: BNC, RCA
        pMF1 -- The passed gender of pType1. ex: m, f

        pType2 -- The passed type of one side of the adapter. ex: BNC, RCA
        pMF2 -- The passed gender of pType2. ex: m, f

        '''

        print 'assigining now'
        side1 = (pType1,pMF1)
        print side1
        side2 = (pType2,pMF2)
        print side2

sideX = ('rca','m')
sideY = ('bnc','f')

x = Adapter(sideX,sideY)
print x.side1
print x.side2

Error: Traceback (most recent call last): File "C:\Users\Cody\Documents\Code\Python\Adapter Finder\adapter.py", line 28, in <module> x = Adapter(sideX,sideY) TypeError: __init__() takes exactly 2 arguments (3 given)

I dont understand what the problem is because I've only entered two args!

Edit: Im new to the python language, though I know Java. Im using this page as a tutorial: http://docs.python.org/tutorial/classes.html

+3  A: 

Your __init__ should look like this:

def __init__(self,(pType1,pMF1),(pType2,pMF2)):
Fu4ny
+9  A: 

Method calls automatically get a 'self' parameter as the first argument, so make __init__() look like:

def __init__(self, (pType1,pMF1),(pType2,pMF2)):

This is usually implicit in other languages, in Python it must be explicit. Also note that it's really just a way of informing the method of the instance it belongs to, you don't have to call it 'self'.

S..
Explained further in the Python doc on [Classes](http://docs.python.org/tutorial/classes.html)
Michael Mrozek
@S.., to have `__init__` display correctly in the first line, you need to place backticks around it. Otherwise the double underscore makes it bold
gnibbler
+2  A: 

Looks like this is the way Python says hello to everyone learning the language. Kind of Python's first bite.

You have to specify self in instance methods as first argument. So it should be.

  def __init__( self, (pType1,pMF1),(pType2,pMF2)):
OscarRyz
+3  A: 

Yes, the OP missed the self, but I don't even know what those tuples-as-arguments mean and I'm intentionally not bothering to figure it out, it's just a bad construction.

Codysehi, please contrast your code with:

class Adapter:
    def __init__(self, side1, side2):
        self.side1 = side1
        self.side2 = side2

sideX = ('rca', 'm')
sideY = ('bnc', 'f')
x = Adapter(sideX, sideY)

and see that it is both more readable, and does what I think you intend.

msw
Thanks! My introduction to python has not gone as smoothly as hoped :PIt makes more sense to me now to just pass in two variables as lists.
codysehl
I found Python a touch bewildering at first, and this was with dozens of languages under my belt. Useful nitpick: `sideX` is bound to a tuple `()` which is immutable as opposed to a list `[]` which is mutable.
msw