About 167,000 results
Open links in new tab
  1. class - What is the difference between objects and classes in …

    Yes, classes (and functions, and modules, and basically everything) in Python are objects, too. The difference lies in their types: class Foo(object): pass print type(Foo) print type(Foo()) To see they're both objects, you can check that they both have attributes: print dir(Foo) print dir(Foo())

  2. python - How to print instances of a class using print ... - Stack …

    Python Print objects in class. 1. Can't print an object from a class. Hot Network Questions I'm owed money ...

  3. Python dictionary from an object's fields - Stack Overflow

    Sep 15, 2008 · Dataclass(from Python 3.7) is another option which can be used for converting class properties to dict. asdict can be used along with dataclass objects for the conversion. Example: @dataclass class Point: x: int y: int p = Point(10, 20) asdict(p) # it returns {'x': 10, 'y': 20}

  4. python - How to create a list of objects? - Stack Overflow

    Aug 15, 2020 · I think what you're of doing here is using a structure containing your class instances. I don't know the syntax for naming structures in python, but in perl I could create a structure obj.id[x] where x is an incremented integer. Then, I could just refer back to the specific class instance I needed by referencing the struct numerically.

  5. How do I correctly clean up a Python object? - Stack Overflow

    May 15, 2009 · You don't want that to happen. You can fix this by creating a PackageResource class that defines the __enter__ and __exit__ methods. Then, the Package class would be defined strictly inside the __enter__ method and returned. That way, the caller never could instantiate the Package class without using a with statement:

  6. Elegant ways to support equivalence ("equality") in Python classes

    Python 3 has only new-style classes that are declared as class A:, class A(object): or class A(B):. For classic-style classes, a comparison operation always calls the method of the first operand, while for new-style classes, it always calls the method of the subclass operand, regardless of the order of the operands .

  7. python - Serializing class instance to JSON - Stack Overflow

    I am trying to create a JSON string representation of a class instance and having difficulty. Let's say the class is built like this: class testclass: value1 = "a" value2 = "b" A call to the

  8. How do you create an incremental ID in a Python Class

    @clsung Python strings are "interned" and they are not mutable. "123" is the same object through your Python session. 'a' and 'b' are named references, not "objects" or "instances" of "123". In this case they point to an interned string. Google python intern for more information. –

  9. sorting - Sort a list of Class Instances Python - Stack Overflow

    Jul 27, 2012 · In addition to the solution you accepted, you could also implement the special __lt__() ("less than") method on the class. The sort() method (and the sorted() function) will then be able to compare the objects, and thereby sort them. This works best when you will only ever sort them on this attribute, however.

  10. python - Saving an Object (Data persistence) - Stack Overflow

    Dec 25, 2010 · Versions > 0 are binary and the highest one available depends on what version of Python is being used. The default also depends on Python version. In Python 2 the default was Protocol version 0, but in Python 3.8.1, it's Protocol version 4. In Python 3.x the module had a pickle.DEFAULT_PROTOCOL added to it, but that doesn't exist in Python 2.

Refresh