Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:11:05+00:00 2026-05-27T23:11:05+00:00

I have a collection of variables in Python, some of which are normal Python

  • 0

I have a collection of variables in Python, some of which are normal Python numbers and some of which are instances of a class I’ve written called Property, which represents a range of possible values (e.g. between 0 and 4.2). I want to iterate over this collection and do something different depending on whether each item is a built-in number or a Property. Of course I could do this by explicitly checking the type, but I want to know if there’s a clean, object-oriented, idiomatic way to do it in Python.

For example, one of the things I want to do depends on the signs of the collection’s entries, so I want to write a function that returns the sign of its argument, regardless of whether it’s passed a built-in type or a Property. I can add a __sign__ method to Property, but there’s no __sign__ method for built-in numeric types, and Python won’t let me add one, so that doesn’t help.

Perhaps I’m just making a mistake by having a collection that mixes class instances with built-in types – but changing it would clutter my code’s interface. Instead of writing

myList = [0, 2.3, 4, Property(0,2)]

I’d have to write something like

myList = [Property(0), Property(2.3), Property(4), Property(0,2)]

which I’d prefer to avoid if possible.

Here’s some more information about what I’m trying to do. I’m working on a scientific problem that involves keeping track of a lot of variables, which interrelate in particular ways. A lot of these variables have known values (because they’ve been measured) but some are unknown and others have been measured but not accurately, so we know they must lie in some particular range. There are equations that specify the interrelationships between these variables, and ultimately I will use a numerical optimisation library to find the possible ranges of the unknown variables.

Because there are a lot of variables I want to store all the known data about them in a sensibly formatted input file. Although I want to keep this a python file for simplicity, it’s important to keep its syntax as simple as possible, both for my sake and because it might have to be read or added to by non-programmers. Currently it has lots of lines that look something vaguely like this:

a.set_some_parameters(3.2, -1, Property(2.3,2.5))
a.x = 0.1
b.x = Property.unknown()
a.set_some_other_parameters(Property.positive(), -4.2, 1, 0)

The point being that (pretty much) anywhere you can enter a number, you can instead enter a Property if the value isn’t precisely known. This works well as an interface, but I end up in the situation described above, where my code continually has to deal with the fact that something might be a built-in number, or it might be a Property instance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:11:06+00:00Added an answer on May 27, 2026 at 11:11 pm

    Without knowing enough about what you’re doing, I’m going to go ahead and agree with your statement

    Perhaps I’m just making a mistake by having a collection that mixes class instances with built-in types

    If you only need to compare the sign between built-in numbers and your Property instances, it wouldn’t be such a big deal (use the cmp function and override comparison operators on your Property class), but I’m betting that’s not all you want to do.

    Instead, think of better ways to construct your list, if that’s what you’re really worried about. Here’s two examples:

    P = Property
    myList = [P(0), P(2.3), P(4), P(0,2)]
    
    # or...
    
    def createPropertyList(*args):
        return [Property(x) for x in args]
    
    myList = createPropertyList(0,2.3,4,(0,2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a private class variable that holds a collection of order items: Private
I have a collection of objects to which I'd like to just add a
I have a collection of classes that inherit from an abstract class I created.
We have a collection of commercial MFC/C++ applications which we sell using Stingray Objective
So I have a list of numbers in python in a list like this
I have a python script called mythicalPythonBindings.py. It outputs something like Establishing database connection
My program will use threads, but some variables like a collection of links/pages, information
I have about 15~20 member variables which needs to be accessed, I was wondering
I have a class named Individual, which has a variable, self.fitness. I have a
I have written code within Python that doesn't release memory the way it should.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.