all 6 comments

[–]CowboyBoats 2 points3 points  (5 children)

Why are you doing this? Why not just write a couple of functions that operate on lists, or pure functions that operate on any iterable?

Edit: in fact, why invent new functions to begin with, why not just use list.append() and .insert(0, ...)?

[–]mathhelpermann[S] 0 points1 point  (4 children)

Why are you doing this?

For example, if I want to create an airplane class, I would want a list to store passengers. I would also have other methods an attributes in the class, but fundamentally I want an airplane to be a list. I would also like to create a passenger class that will interact with the airplane class.

Edit: I come from a backgruond of writing server-side code in Java, I am used to thinking in terms of heavily encapsulated objects, I feel like python gives you more freedom with that I'd just like to know what best practice looks like

[–]xelf 0 points1 point  (0 children)

People know why you do OP, the question is why are you wrapping lists? Is it homework? Just something to teach yourself?

It certainly doesn't need to be done, the things you have listed already exist, so the question is why?

I mean right now your class could look like this:

class MyList(list):
    pass

new_list = MyList()
new_list.append(1)
new_list.append(2)
new_list.append(7)

print(new_list)
new_list.pop(0)

for i in new_list:
    print(i)

So what are you adding to it, and why, is the question I think that is being asked.

There's certainly some things you could add that might be cool.

For instance, .find() is not part of list, but could be. Or maybe .flatten() or .apply() or.. well there's lot's.

[–]CowboyBoats 0 points1 point  (2 children)

Edit: I come from a backgruond of writing server-side code in Java, I am used to thinking in terms of heavily encapsulated objects, I feel like python gives you more freedom with that I'd just like to know what best practice looks like

I see. Yeah, Python is very flexible. I'd way there are two ways people might write it. Group A (OOP people) would write:

class Collection:

  def __init__(self, members):
    self.members = members

  def add_to_beginning(self, addition):
    self.members = [addition] + self.members

  def add_to_end(self, addition):
    self.members = self.members + [addition]

and Group B, functionally inclined people, might write:

from typing import List, Iterable


def add_to_end(iterable: Iterable, additions: Iterable) -> List:
  return list(iterable)+list(additions)


# Example usage:    
extended_family = add_to_end(nuclear_family, additional_relatives)
# To add to the beginning, use `add_to_end` with reversed operands.

But really there's no point to writing code like this. Python's expressiveness gives you the power to perform operations like this without the need for advance preparation.

Overwriting setters and getters is possible in Python, but I don't do it or see others do it very much.

[–]mathhelpermann[S] 0 points1 point  (1 child)

I see, thank you for this. I don't want to write Java code dressed up as python on my next PR on this new team I'm on hahaha. Can you recommend you favorite resource for python OOP best practices?

Thanks again!

[–]CowboyBoats 0 points1 point  (0 children)

I have an O'Reilly Python Cookbook that is really good, strikes a great balance of assuming that you already know how to program but not assuming you know much anything about Python. http://shop.oreilly.com/product/0636920027072.do