Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error instead of raise error? #1

Open
the-zebulan opened this issue Aug 5, 2016 · 2 comments
Open

Return error instead of raise error? #1

the-zebulan opened this issue Aug 5, 2016 · 2 comments

Comments

@the-zebulan
Copy link

the-zebulan commented Aug 5, 2016

I'm not exactly sure this is an issue but I'm going through your Udemy course right now (great course by the way!) and I was wondering about this part.

Dynamic Array Exercise.ipynb

def __getitem__(self, k):
    """
    Return element at index k
    """
    if not 0 <= k < self.n:
        return IndexError('K is out of bounds!') # Check it k index is in bounds of array

    return self.A[k] #Retrieve from array at index k

Is there a particular reason why you use return IndexError instead of raise IndexError? I've searched a bit on the internet but cannot find a similar example. I'm only familiar with using raise IndexError and this is the first time I've seen an error returned like that.

Thanks!

EDIT:

Just noticed a potential typo in your code as I was re-reading my comment.

# Check it k index
        ^^
#       if ?
@cowboyuniverse
Copy link

i'm using this code to see if it's doubling but it's not doing that

import sys n=10 for i in range(n): # Number of elements a = len(arr) # Actual Size in Bytes b = sys.getsizeof(arr) print 'Length: {0:3d}; Size in bytes: {1:4d} '.format(a,b) arr.append(n)

Length: 2; Size in bytes: 64 Length: 3; Size in bytes: 64 Length: 4; Size in bytes: 64 Length: 5; Size in bytes: 64 Length: 6; Size in bytes: 64 Length: 7; Size in bytes: 64 Length: 8; Size in bytes: 64 Length: 9; Size in bytes: 64 Length: 10; Size in bytes: 64

@vissree
Copy link

vissree commented Jul 20, 2017

@cowboyuniverse

sys.getsizeof(arr) calculates the size of the DynamicArray() object itself - not the array size. The solution is to create a sizeof function which returns the actual size of the array (in bytes).

def __sizeof__(self):
        """
        Return the size of the object
        """
        
        return self.capacity * ctypes.sizeof(ctypes.py_object)

https://docs.python.org/3/library/sys.html#sys.getsizeof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants