![]() The first parameter to _init_ is the new object we're initializing, which we usually call "self" so that people understand what we're doing. In our _init_, we generally just assign the starting values for attributes. In this case, construct a Vector of the specified length with each element is initialized to 0.0. You can look it up for yourself in the documentation (or try help(isinstance) at the REPL). This is tested by using the builtin function isinstance. If this argument is either an int or a long or an instance of a class derived from one of these The constructor should take a single argument. In Python, the common "this is how you create a new object and say what it's an instance of" stuff is handled internally by default, and then the baby object is passed to the class' initialization method to make it into a "proper" instance, by setting the attributes that new instances of the class should have. But then, this sort of thing is normal for academic exercises, sadly. ![]() ) The entire concept for the class is pretty silly real Python programmers just use the built-in sequence types directly. Your instructor seems not to "speak Python as a native language". Could someone please help me with this? Thanks! I'm not sure how to do the class type checking, as well as how to initialize the vector based on the given object. This string of code should consist of the name of the class followed by an open parenthesis followed by the contents of the vector represented as a list followed by a close parenthesis." Next implement the _repr_ method to return a string of python code which could be used to initialize the Vector. If the argument is not used as the length of the vector and if it is not a sequence, then raise a TypeError with an appropriate message. If the argument is not considered to be the length, then if the argument is a sequence (such as a list), then initialize with vector with the length and values of the given sequence. If the length is negative, raise a ValueError with an appropriate message. If this argument is either an int or a long or an instance of a class derived from one of these, then consider this argument to be the length of the Vector instance. But I'm new to python and don't really know how to crack it. I'm working on this project which deals with vectors in python.
0 Comments
Leave a Reply. |