Python Forum
Limiting valid values for a variable in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Limiting valid values for a variable in a class
#1
Is it possible to limit the values of a variable of a class? Say I have a class Vehicle with variables Type, Make, Model, etc and I want to limit the values of Type to "sedan", LDV, SUV and 4X4.
Reply
#2
Not sure I really understand the question but, you can a have a predefined list of types. If it's not in the list throw a message instead of setting the variable.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You can do it by defining a property

class Foo:
    _spam_values =  set(["sedan", "LDV", "SUV", "4X4"])

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

    @property
    def spam(self):
        return self._spamspamspam

    @spam.setter
    def spam(self, value):
        if value not in self._spam_values:
            raise ValueError('Invalid spam value', value, 'Expected one of', self._spam_values)
        self._spamspamspam = value

foo = Foo('LDV')
foo.spam = 'SUV'   # OK
foo.spam = 'ham'   # KO, raises ValueError
It could be sytematized somewhat by using the descriptor protocol:
class ConstrainedMember:
    def __init__(self, name, values, store_name=None):
        self.name = name
        self.values = set(values)
        self.store_name = store_name or ('_' + name)
        
    def __get__(self, obj):
        return getattr(obj, self.store_name)
    
    def __set__(self, obj, value):
        if value not in self.values:
            raise TypeError('Invalid value for member', self.name,
                            'Expected one of', self.values,
                            'got', value)
        setattr(obj, self.store_name, value)
        
class Foo:
    spam = ConstrainedMember('spam', ["sedan", "LDV", "SUV", "4X4"])
    
foo = Foo()
foo.spam = 'SUV'
foo.spam = 'bar'
Reply
#4
Thank you!!
Reply
#5
i am new comer here....how can i post here about python related problem.
Reply
#6
@Monira Read the "Posting a new thread" topic in the Help/Rules page.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script getting reindexing only valid error cubangt 1 798 Dec-07-2023, 04:06 PM
Last Post: cubangt
Question Use function, retry until valid Ashcora 8 1,406 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  Variable Types in a class nafshar 9 2,363 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  store all variable values into list and insert to sql_summary table mg24 3 1,097 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,773 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Compare variable with values in a file paulo79 1 1,079 Apr-22-2022, 03:16 AM
Last Post: Pedroski55
  How to add for loop values in variable paulo79 1 1,411 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,594 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Calling a base class variable from an inherited class CompleteNewb 3 1,595 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  checking for valid hexadecimal digits Skaperen 3 6,269 Sep-02-2021, 07:22 AM
Last Post: buran

Forum Jump:

User Panel Messages

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