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 8532619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:45:23+00:00 2026-06-11T09:45:23+00:00

I constructed a class: class Foo (object): def __init__(self,List): self.List=List @property def numbers(self): L=[]

  • 0

I constructed a class:

class Foo (object):
      def __init__(self,List):
          self.List=List
      @property
      def numbers(self):
          L=[]
          for i in self.List:
              if i.isdigit():
                 L.append(i)
          return L
      @property
      def letters(self):
          L=[]
          for i in self.List:
              if i.isalpha():
                 L.append(i)
          return L

>>> inst=Foo(['12','ae','45','bb'])
>>> inst.letters
['ae', 'bb']
>>> inst.numbers
['12', '45']

How can I add attributes so I could do inst.numbers.odd that would return ['45']?

  • 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-06-11T09:45:24+00:00Added an answer on June 11, 2026 at 9:45 am

    Your numbers property returns a list, so a numbers.odd won’t work.

    However, you could follow a workflow like:

    • define a small class Numbers, that would define two properties even and odd
      For example, Numbers could take a list as argument of its __init__, the even property would return only the even number of this list [i for i in List if int(i)%2 == 0] (and odd the odd ones)…

    • create an instance of Numbers in your Foo.numbers property (using your Foo.List to initialize it) and return this instance…

    Your Numbers class could directly subclass the builtin list class, as suggested. You could also define it like

    class Numbers(object):
        def __init__(self,L):
            self.L = L
        @property
        def even(self):
            return [i for i in self.L if not int(i)%2]
        def __repr__(self):
            return repr(self.L)
    

    Here, we returning the representation of Numbers as the representation of its L attribute (a list). Fine and dandy until you want to append something to a Numbers instance, for example: you would have to define a Numb.append method… It might be easier to stick with making Numbers a subclass of list:

     class Numbers(list):
        @property
        def even(self):
            ...
    

    Edited: corrected the // by a %, because I went too fast and wasn’t careful enough

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
Consider the file bug.scala : package sandbox object Foo { implicit def stringToInt(s: String)
I have a type like this sealed class Foo[A](val value: A) object Foo {
In Scala, we can write object Foo { def bar = {} } How
Suppose I have a the following in Scala object Foo { var functions: List[String
I'm reviewing some old python code and came accross this 'pattern' frequently: class Foo(object):
Say, I have the following class in Python class Foo(object): a = None b
I have a constructor method similar to this: public class Foo { public Foo
I have constructed a class to mimic a C# struct: public class Favourite {
The TimeSpan class can be constructed with days, hours, minutes, seconds and milliseconds: public

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.