0

i am learning python by myself. Right now i try to code a address book. I read that you should use a class for it and i don't get why because you could define every function you have in this class (like insertaddress, deletaddres,... ) without one and use it like normal functions. Or am i wrong?

But to get some practice with classes i created one:

class Addressbook():

    def __init__(self,last,first,street,num,plz,place):

        self.last=last
        self.first=first
        self.street=street
        self.num=num
        self.plz=plz
        self.place=place

    def insertadd(self):
        text =self.last+"    "+self.first+"    "+self.street+"    
        "+self.num+"    "+self.plz+"    "+self.place+"\n"
        with open("addressbook.txt", "a") as file:
            file.write(text)

In my main-file:

from klassen import Addressbook

k.last= input()
k.first=input()
(...)
k.place = input()

k.insertadd()

But when i import my class via from klassen import Addressbook as k and link the class attributes to my input: k.first=input("text")and try to execute the function k.insertadd() i get an errror message.

NameError: name 'self' is not defined

When i give my function every paramter i have as an input i get an error which tells me that "place" is a missing paramter.

Could you please help me?

Omikron
  • 3
  • 4
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Jun 01 '17 at 16:50
  • 2
    You appear to have some misunderstandings about how classes work. It looks like you're trying to use the class object directly to hold your data. Don't do that. You need to create one or more instances of the class. And for the time being forget about importing the Addressbook class. Try to get this working in a single script file first. Once it all works you may want to split it up into multiple files, but there's probably no need to do that, Python isn't Java. – PM 2Ring Jun 01 '17 at 17:02
  • My friend codes in c++ and he told me that i could structure my code more by creating extra files for classes and functions. so that my main will be better to read. Do you mean no multiple files for smaller projects or in general`? – Omikron Jun 01 '17 at 19:01

2 Answers2

0

You should do:

from klassen import Addressbook
k = Addressbook("","","","","","") # Init it with the variables you want!

Then you can use k as an object...

The way you were doing, you were not creating a instance of an Addressbook object, you were using the class object itself...

DSLima90
  • 2,680
  • 1
  • 15
  • 23
0

It is generally accepted to use a class when a certain set of functions are to be encapsulated together or the functionality is to be abstracted.

Imagine that your "address book" contains a set of functions used by various different applications. For example, by a "phone book", "messaging application" and so on. By segregating each of them as a class, you have greater control over the code and the associated resources, with less redundancy. Therefore, maintainability improves.

For your code, you have to initialize all the variables. Or at least pass them to your init function.

from klassen import Addressbook

k = Addressbook("bar","foo","15 XYZ street",1234444212,"ABC","New York")

k.insertadd()

Or the other way of handling it is to initialize in the init function to accept "None" parameter as default.

Eg: class Addressbook():

def __init__(self,last,first,street = None,num,plz = None,place = None):

    self.last=last
    self.first=first
    self.street=street
    self.num=num
    self.plz=plz
    self.place=place

Now the object would not need all the parameters.

from klassen import Addressbook

k = Addressbook(last = "bar", first = "foo", num = 1234444212)

k.insertadd()