1

For an assignment I have to write a program that can create directories and files, we have to write a DirectoryEntry class to help do this, and for part of it we have to create a name for the file or directory. If a name is entered then we just use the name but if no name is entered we just use 9 spaces.

I don't know how to do this using the __init__ method because Python3 doesn't allow you to have more then one constructor method.

Right now it just looks like:

def __init__(self, name):
 self.type = "f:"
 self.name = name
 self.length = "0000"
 self.colon = ":"
 self.blocks = ["000", "000", "000", "000", "000", "000", "000", "000", "000", "000", "000", "000"]` 
asked Sep 8, 2016 at 9:01
1
  • 1
    You can have a default parameter, provided it is not a mutable object. For example: def __init__(self, name=' '):. However, is 9 spaces a sensible filename? Commented Sep 8, 2016 at 9:04

4 Answers 4

3

You can use:

def __init__(self, name='your_default_name'):

Then you can either create an object of that class with my_object() and it will use the default value, or use my_object('its_name') and it will use the input value.

answered Sep 8, 2016 at 9:04
Sign up to request clarification or add additional context in comments.

Comments

1

Just set it like this, so for default will use 9 spaces

def __init__(self, name=' '):
 self.type = "f:"
 self.name = name
 self.length = "0000"
 self.colon = ":"
 self.blocks = ["000", "000", "000", "000", "000", "000", "000", "000", "000", "000", "000", "000"]

More information

answered Sep 8, 2016 at 9:04

Comments

1

Specify it as a default variable as shown below

def __init__(self, name=' '*9):
 self.type = "f:"
 self.name = name
 self.length = "0000"
 self.colon = ":"
 self.blocks = ["000", "000", "000", "000", "000", "000", "000","000","000","000", "000", "000"]
answered Sep 8, 2016 at 9:06

1 Comment

They could shorten self.blocks = ["000"] * 12 as well.
0

You can use default arguments:

def __init__(self, name=' '):
answered Sep 8, 2016 at 9:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.