Class

Fredrik Lundh fredrik at pythonware.com
Wed Nov 7 08:30:16 CET 2001


Edy Lie wrote:
> Hi I have setup a class but in the code i could not use it. Anyone care to
> explain ?

hint: it's usually easier to for the readers of this forum to figure
out what's wrong if you include the error message.

> class DataBase:
>    def __init__(self, Plan, Domain, Email, Name):
>       self.UserPlan = Plan
>       self.UserDomain = Domain
>       self.UserEmail = Email
>       self.UserName = Name

> if choice == 1:
>     print 'Adding New User\n'
>     add = DataBase()

did you get this message?

Traceback (most recent call last):
  File "\qq.py", line 19, in ?
    add = DataBase()
TypeError: __init__() takes exactly 5 arguments (1 given)

this means pretty much what it says: the __init__ method
(which is called automatically when you create the object)
expects five arguments, but only got one (the implied self
argument).

> class DataBase:
>    def __init__(self, Plan, Domain, Email, Name):

you can either modify the call to something like:

    # pass in None as default for all attributes; we'll
    # set them below anyway
    add = DataBase(None, None, None, None)

>     add.UserPlan = input("Username: ")
>     add.UserDomain = raw_input("Domain: ")
>     add.UserEmail = raw_input("Email: ")
>     add.UserName = raw_input("Name: ")

or read in the values before creating the object:

    plan = input("Username: ")
    domain = raw_input("Domain: ")
    email = raw_input("Email: ")
    name = raw_input("Name: ")
    add = DataBase(plan, domain, email, name)

or perhaps modify the class in some way.

hope this helps!

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list