Why does init need two arguments? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm probably missing something obvious here. With the following code:
class Thing():
def __init__(self, name):
self.name = name
that = Thing()
I get the error 'init needs two arguments'. I thought 'self' was one of the arguments and when I try to instantiate the object by putting the name inside the parentheses I get other errors.

Yes, your __init__ takes two arguments: self, and name. When you call Thing(), self is passed implicitly, but you still need to pass the second one explicitly, like Thing("name"). If you're still getting an error when doing that, that's a different story. You should post that error as well.
(And I doubt the error says "init needs two arguments". It would have been more helpful to include the actual error message...)

Related

Variable naming conventions for function variables [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
beginner here. I'm writing a script that has a few related functions and am wondering what the standards are for naming the variables within my functions. For example if I have function_1 and function_2 that both a take some sort of file, is it acceptable to name both variables file? I know it will work, but is that horrible coding practice or is it alright to do?
def function_1(file):
# Do something
return file
def function_2(file):
# Do something
return file
def main():
file_1 = function_1(file)
file_2 = function_2(file)
if __name__ == "__main__":
main()
Since the two are parameters within the scope of two different functions, it is completely fine to name them the same. However, since file is a builtin in Python, I would suggest to name it differently or to add an underscore at the end of the name.

Where are python operators defined? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Where are the python operators like +, -, * defined? I am a newbie, so please give a detailed answer to this question.
every Python class has built-in methods (can be recognized by the "__" in the beginning and end of their name) that define their behavior. for example, when using print() on an object, the built-in __str__ function is called, and it is different for every class.
you can override these functions with your own implementations.
here is a class named CarCollection:
class CarCollection():
def __init__(self, car_list):
self.cars_in_collection = car_list
now, say for example you want to add two collections together. using the "+" between two instances of this class will raise TypeError: unsupported operand type(s) for +: 'instance' and 'instance', so in order to add two collections together you need to override the __add__ function:
def __add__(self, other_car_collection):
return self.cars_in_collection + other.cars_in_collection
now when you add two collections together and print the result it will look like this:
first_collection = CarCollection(["subaru", "ferrari"])
second_collection = CarCollection(["fiat", "renault"])
print(second_collection + first_collection)
output: subaru, ferrari, fiat, renault

Why should a python variable of type list should be defined first before assigning a value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
x = 2
creates an integer variable.
y.append(3)
gives an error message (name 'y' is not defined).
In the first one a variable x can be assigned a value without first defining it, why can't we do the same thing with the lists (you have to first define it using l = []). Is this the result of a fundamental design choice in the Python language?
Thanks.
The actual act of assignment also creates the variable if it doesn't exist.
When you attempt to call a member function the variable needs to exist, so the Python interpreter knows what kind of variable it is (the type of the variable). If the interpreter doesn't know the kind of the variable, it can't know what member functions exist on the object.

Two vectors inside a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new on Python.
I want to create a function with two vectors inside. I tried like this
def twovectors((velocity1,length1),(velocity2,length2)):
but I have a message error like
SyntaxError: invalid syntax.
Please, need help.
You cannot put tuple in the function definition as a parameter. Check Multiple Function Arguments or 8.6. Function definitions in the Python language reference.
Try something like this this:
def twovectors(vector1, vector2):
velocity1, length1 = vector1
velocity2, length2 = vector2
# Other code...
I used tuple unpacking to expand provided tuple arguments.
You write functions in python in this way :
def twovectors(velocity1, velocity2):
# You can get the length of those vectors after you get inside the function
len1, len2 = len(velocity1), len(velocity2)
// Your code here
return whateveryouwantto

Modify docstring in a decorator [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am using flask, and some functions have decorators, to check the presence of some headers and return some error codes if they are missing.
In these decorators, before returning the decorated function i do something like
decorated_function.__doc__ += "Returns 400 if the X-Version header is not present."
Is this pythonic? Is there a better way to achieve it?
I am using wraps already from functools.
def ModDoc(doc):
def wrapped(func):
func.__doc__ = doc
return func
return wrapped
#ModDoc("test2")
def test():
"""test"""
return
print test.__doc__
Will modify the docstring of anything it is applied too. Remember these changes are purely interactive, and will not show up in stored or auto generated documentation.

Categories

Resources