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"]`
4 Answers 4
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.
Comments
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"]
Comments
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"]
1 Comment
self.blocks = ["000"] * 12 as well.You can use default arguments:
def __init__(self, name=' '):
Comments
Explore related questions
See similar questions with these tags.
def __init__(self, name=' '):. However, is 9 spaces a sensible filename?