Python Forum
How to check if class instance exists in a list of class instance objects?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if class instance exists in a list of class instance objects?
#21
(May-26-2018, 08:22 PM)buran Wrote: instances of classes and instances of objects???

Classes in Python are objects too
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#22
Of course everything is an object, no doubt about that!!! "
That's exactly why I don't get the difference you make between instances of classes and instances of objects...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#23
(May-27-2018, 03:58 AM)buran Wrote: Of course everything is an object, no doubt about that!!! "
That's exactly why I don't get the difference you make between instances of classes and instances of objects...

Does "object of un-instantiated class" sounds better? If that is still unclear

Output:
In [31]: type(list().__class__) Out[31]: type In [32]: type(dict().__class__) Out[32]: type In [33]: type('abc'.__class__) Out[33]: type
Classes itself are objects of type .... type, which is a trinity Cool of function, metaclass and class factory.

Even if object is un-hashable, the class of that object will be always hashable (unless you implement some crazy hack that will make it unhashable - if that is possible). Again, look at my code: I was not saving objects in a set - I was saving classes


PS Maybe, a little demonstration will help

Output:
In [37]: class A: ...: def __init__(self, name): ...: self.name = name ...: In [38]: set([A('volcano63').__class__, A('buran').__class__, A('sonicblind').__class__]) Out[38]: {__main__.A}
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#24
(May-27-2018, 04:32 AM)volcano63 Wrote: Does "object of un-instantiated class" sounds better?
Yes, now I see what you mean
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 389 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 257 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 296 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 369 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 410 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Operation result class SirDonkey 6 536 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 670 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 740 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 486 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,842 Jan-24-2024, 08:36 AM
Last Post: annakenna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020