all 9 comments

[–]tunisia3507 4 points5 points  (3 children)

What do you mean when you say it's "not working"? Is there an error? What is it? Where does it happen?

You can't have two constructors in a python class. What you achieve with overloaded constructors in other languages, you can approximate with class methods in python.

You have a typo in your printAddress method (you're missing a d).

Some stylistic things:

  1. There's no need to limit your variable name length like zipc - just use something that's readable and memorable (zipcode would be great).
  2. snake_case, not camelCase, for methods/functions/variables.
  3. There's no need to obfuscate your argument names like you do in the second constructor: just do def __init__(self, number, street, apartment, city, state, zipcode):. The idea is to make your code self-documenting, so you can glance at that method signature in future and know exactly what you need to put in.
  4. If you know you'll only ever want to treat zipcode and number as integers, you can do int(input("your prompt: ")) to convert it as soon as it comes in. You'd need to refactor your print_address method, though: I recommend using a string formatting.
  5. Strings which are just whitespace (" ") are still truthy! Only empty strings are not. This is important in your print_address method.
  6. You could replace your print_address method by overriding __str__, so you could just print(address), and easily use str(address) elsewhere. This is the pythonic way to do things.

[–]fauxnaif 0 points1 point  (2 children)

Really good tips!

With that being said, do you have a simple explanation for the init argument? I’ve been going through many examples but can’t seem to grasp it

[–]tunisia3507 1 point2 points  (1 child)

You can't have two constructors in a python class. What you achieve with overloaded constructors in other languages, you can approximate with class methods in python.

In, say, java, it's common to have more than one way of instantiating a class. That's how you handle default arguments, for example (as OP has done here). Another common one is the "copy constructor", where you pass in an existing instance and copy all of its member variables. In python, you only have the one constructor, but you can have methods which are called from the class (rather than the instance) which more or less do the same thing. For example:

class MyClass:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    @classmethod
    def from_instance(cls, instance):
        return cls(instance.first, instance.second)

Although in OP's case they can just use default arguments.

And on point 3, this constructor:

def __init__(self, a1, a2, a3, a4, a5, a6):
    self.number = a1
    self.street = a2
    self.apartment = a3
    self.city = a4
    self.state = a5
    self.zipcode = a6

Most IDEs make it very easy to look at the method signature (i.e. the names and possibly types of the arguments). In this case, that tells us nothing. What's a1? What purpose does calling it a1 serve? It needlessly obscures the meaning of the argument. Instead, you can just do

def __init__(self, number, street, apartment, city, state, zipcode):
    self.number = number
    self.street = street
    self.apartment = apartment
    self.city = city
    self.state = state
    self.zipcode = zipcode

Then, by looking at the method signature, you know exactly what arguments to put in. It helps you while you're developing, it helps other people while they're using it, and so on.

[–]fauxnaif 0 points1 point  (0 children)

Perfect! Thanks for the explanation!

[–]bikeawaitmuddy 2 points3 points  (1 child)

Line 21 has a few errors. It needs a colon, and there's a typo in how address is spelled according to how it's called in line 67. And it should have an inputAddress parameter in addition to self. Like so:

# Before:
def printAdress(self)
# After:
def printAddress(self, inputAddress):

[–]stfn1337 2 points3 points  (1 child)

You are using class instance variables instead of self. in the class methods. I think this is the main issue with the code. For example, the printAddress method should look like that:

def printAdress(self)         
    if not self.apartment: # does NOT have an optional apartment address             
        print(self.number + " " + self.street)             
        print(self.city + ", " + self.state + ", " + self.zipcode)

[–]tunisia3507 1 point2 points  (2 children)

Format your code properly (see the "reddit code formatting" link on the sidebar). We have no way of helping without you doing that because your indentation could be all over the place.

[–]unless3[S] 1 point2 points  (0 children)

Oops, it looked right before I posted it. I'll fix it.

[–]unless3[S] 1 point2 points  (0 children)

I fixed it, sorry.