-3

I have an object that has int and string, i.e.:

obj1 = Object("cat",(1,2),(2,2))

I want to create an array that could store obj1. I have searched online and realized that Python array could only contain 1 data type.

However, is there any other ways to create an array that could store an object with different data type?

Thank you

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 4
    Do you mean Python `list`? Of course it can store objects of various types... – Andrej Kesely Jul 22 '19 at 08:08
  • 10
    ***"I have searched online and realized that Python array could only contain 1 data type. "*** - Search again. Here's 3 datatypes (int, float and string) inside the same `list`: `a = [1, 2.2, 'python']` – Pedro Lobito Jul 22 '19 at 08:09
  • Try pasting `my_list = ["cat", (1,2), (2,2)]` into a python interpreter. It's completely valid Python. – Boris Verkhovskiy Jul 22 '19 at 08:12
  • @AndrejKesely Sorry I mean Array. Is it possible to store the object to an array? – user10993820 Jul 22 '19 at 08:20
  • 3
    What, **exactly** do you mean by array? Generally, you would use a `list` in Python, arrays being rather esoteric in the language. If you want to store heterogenous data types, then you should *almost certainly* be using a `list`. – juanpa.arrivillaga Jul 22 '19 at 08:20
  • Do you mean an array defined by the [`array`](https://docs.python.org/3/library/array.html#module-array) module? All the elements in one have to be the same numeric type. – martineau Jul 22 '19 at 09:42

2 Answers2

2

You can use python list:

obj1 = Object("cat",(1,2),(2,2))
my_objects = []
my_objects.append(obj1)
Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
  • @Paolo Mossini May I ask let say in my_objects, how do I print out obj1's value? I tried to print but it returns [<__main__.Object object at 0x031C7050>] – user10993820 Jul 22 '19 at 12:50
  • @Paolo Mossini Nope it doesn't work, it shows me '[<__main__.Object object at 0x031C7050>]' – user10993820 Jul 22 '19 at 12:59
  • @PaoloMossini May I know how I can paste the code as it is? – user10993820 Jul 22 '19 at 13:14
  • class Object: # class constructor def __init__(self,name,topL,bottomR): self.name = name self.topL = topL self.bottomR = bottomR – user10993820 Jul 22 '19 at 13:35
  • To print the object values you can use: `print(my_objects[0].name)` `print(my_objects[0].topL)` `print(my_objects[0].bottomR)` – Paolo Mossini Jul 22 '19 at 13:50
  • @PaoloMossini Is there a way if I print(my_objects[0]), it will return [0]'s all values instead of printing one at a time? Thank you – user10993820 Jul 22 '19 at 14:02
  • check this [link](https://stackoverflow.com/questions/192109/is-there-a-built-in-function-to-print-all-the-current-properties-and-values-of-a) and let me know if it works – Paolo Mossini Jul 22 '19 at 14:08
  • 1
    @PaoloMossini Hi it did not work for me, but I have created a function that will display all values when I call the function. Thanks for helping me!! – user10993820 Jul 23 '19 at 09:12
0

I assume that your mention of "Python array could only contain 1 data type" relates to the array package of the sdtlib, which is indeed quite a specialized collection type. The generic builtin "array" type in Python is actually named list - as you would have known if you had done the official tutorial -, and can of course contain just any kind of object.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118