Actually None
is much better for “magic” values:
class Cheese(): def __init__(self, num_holes = None): if num_holes is None: ...
Now if you want complete freedom of adding more parameters:
class Cheese(): def __init__(self, *args, **kwargs): #args -- tuple of anonymous arguments #kwargs -- dictionary of named arguments self.num_holes = kwargs.get('num_holes',random_holes())
To better explain the concept of *args
and **kwargs
(you can actually change these names):
def f(*args, **kwargs): print 'args: ', args, ' kwargs: ', kwargs >>> f('a') args: ('a',) kwargs: {} >>> f(ar='a') args: () kwargs: {'ar': 'a'} >>> f(1,2,param=3) args: (1, 2) kwargs: {'param': 3}
http://docs.python.org/reference/expressions.html#calls
Related Posts:
- ImportError: DLL load failed: The specified module could not be found
- Replacements for switch statement in Python?
- Correct way to write line to file?
- How to read a file line-by-line into a list?
- How do I sort a dictionary by value?
- TypeError: only size-1 arrays can be converted to Python scalars (matplotlib)
- Why do I get TypeError: can’t multiply sequence by non-int of type ‘float’?
- Writing a pandas DataFrame to CSV file
- How to install pytorch in Anaconda with conda or pip?
- How can I remove a key from a Python dictionary?
- What does numpy.random.seed(0) do?
- How do I check if a variable exists?
- Is there a label/goto in Python?
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- Getting TypeError: __init__() missing 1 required positional argument: ‘on_delete’ when trying to add parent table after child table with entries
- Python Pandas Counting the Occurrences of a Specific value
- What does the c underscore expression `c_` do exactly?
- Count unique values per groups with Pandas
- Get unique values from a list in python [duplicate]
- How do you decode Base64 data in Python?
- Convert pandas Series to DataFrame
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- Error: all the input array dimensions except for the concatenation axis must match exactly
- Expected 2D array, got 1D array instead error
- How to prevent errno 32 broken pipe?
- The equivalent of a GOTO in python [duplicate]
- tkinter gui with progress bar
- What is the difference between Jupyter Notebook and JupyterLab?
- Python 3 ImportError: No module named ‘ConfigParser’
- How does Python’s super() work with multiple inheritance?
- How can the Euclidean distance be calculated with NumPy?
- Tkinter understanding mainloop
- Embedding in pytorch
- Check string “None” or “not” in Python 2.7
- re.search().TypeError: cannot use a string pattern on a bytes-like object
- Cannot uninstall Tensorflow
- Add Legend to Seaborn point plot
- Get key by value in dictionary
- How to add multiple values to a dictionary key in python?
- Where can I find a list of the Flask SQLAlchemy Column types and options?
- How can I install Python’s pip3 on my Mac?
- convert json ipython notebook(.ipynb) to .py file
- how to update spyder on anaconda
- pygame clock.tick() vs framerate in game main loop
- Use of “global” keyword in Python
- Convert a Pandas DataFrame to a dictionary
- Python: Write array values into file
- use np.random.multinomial() in python
- pg_config executable not found
- Import error when trying to import tensorflow with gpu
- Removing Punctuation From Python List Items
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “OverflowError: Python int too large to convert to C long” on windows but not mac
- Create a Pandas Dataframe by appending one row at a time
- Python Array Rotation
- standard_init_linux.go:178: exec user process caused “exec format error”
- How to find char in string and get all the indexes?
- How can I create an array/list of dictionaries in python?
- How to unnest a nested list
- How to sort a list/tuple of lists/tuples by the element at a given index?
- What can lead to “IOError: [Errno 9] Bad file descriptor” during os.system()?
- Shebang doesn’t work with python3
- How do I make a dictionary with multiple keys to one value?
- Directing print output to a .txt file
- How to get a value from a cell of a dataframe?
- TypeError: can only concatenate str (not “float”) to str
- Converting a sentence to piglatin in Python
- How to use pygame.KEYDOWN to execute something every time through a loop while the key is held down?
- How to create an integer array in Python?
- Recursion in Python? RuntimeError: maximum recursion depth exceeded while calling a Python object
- Writing a dictionary to a csv file with one line for every ‘key: value’
- How to remove square brackets from list in Python?
- Errno 10061 : No connection could be made because the target machine actively refused it ( client – server )
- write multiple lines in a file in python
- JSON object must be str, bytes or bytearray, not dict
- How can I create an object and add attributes to it?
- Checking to see if a string is an integer or float
- Getting the name of a variable as a string
- can’t multiply sequence by non-int of type ‘list’
- cannot convert the series to
- coercing to Unicode: need string or buffer, NoneType found when rendering in django admin
- Convert from ASCII string encoded in Hex to plain ASCII?
- Simple way to create matrix of random numbers
- pandas: multiple conditions while indexing data frame – unexpected behavior
- ‘instancemethod’ object has no attribute ‘__getitem__’ with class variables
- Python/Scikit-Learn – Can’t handle mix of multiclass and continuous
- Sorting list of lists by the first element of each sub-list
- Install py2exe for python 2.7 over pip: this package requires Python 3.3 or later
- What is the correct format to write float value to file in Python
- python-How to set global variables in Flask?
- How do I install pip on macOS or OS X?
- NameError: name ‘get_ipython’ is not defined
- Equivalent to matlab’s imagesc in matplotlib? [duplicate]
- bash: syntax error near unexpected token `(‘ – Python
- Find all files in a directory with extension .txt in Python
- “public” or “private” attribute in Python ? What is the best way?
- How to search for a string in text files?
- TypeError: ‘zip’ object is not subscriptable
- Pip: could not find a version. No matching distribution found
- Compare two columns using pandas