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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:50:42+00:00 2026-05-26T07:50:42+00:00

I have a class WeightedArc defined as follows: class Arc(tuple): @property def tail(self): return

  • 0

I have a class WeightedArc defined as follows:

class Arc(tuple):

  @property
  def tail(self):
    return self[0]

  @property
  def head(self):
    return self[1]

  @property
  def inverted(self):
    return Arc((self.head, self.tail))

  def __eq__(self, other):
    return self.head == other.head and self.tail == other.tail

class WeightedArc(Arc):
  def __new__(cls, arc, weight):
    self.weight = weight
    return super(Arc, cls).__new__(arc)

This code clearly doesn’t work, because self isn’t defined for WeightArc.__new__. How do I assign the attribute weight to the WeightArc class?

  • 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-26T07:50:43+00:00Added an answer on May 26, 2026 at 7:50 am

    The fixed-up version of your original code is:

    class WeightedArc(Arc):
        def __new__(cls, arc, weight):
            self = tuple.__new__(cls, arc)
            self.weight = weight
            return self
    

    Another approach to look at the verbose option for collections.namedtuple to see an example of how to subclass tuple:

    >>> from collections import namedtuple, OrderedDict
    >>> _property = property
    >>> from operator import itemgetter as _itemgetter
    >>> Arc = namedtuple('Arc', ['head', 'tail'], verbose=True)
    class Arc(tuple):
        'Arc(head, tail)' 
    
        __slots__ = () 
    
        _fields = ('head', 'tail') 
    
        def __new__(_cls, head, tail):
            'Create new instance of Arc(head, tail)'
            return _tuple.__new__(_cls, (head, tail)) 
    
        @classmethod
        def _make(cls, iterable, new=tuple.__new__, len=len):
            'Make a new Arc object from a sequence or iterable'
            result = new(cls, iterable)
            if len(result) != 2:
                raise TypeError('Expected 2 arguments, got %d' % len(result))
            return result 
    
        def __repr__(self):
            'Return a nicely formatted representation string'
            return 'Arc(head=%r, tail=%r)' % self 
    
        def _asdict(self):
            'Return a new OrderedDict which maps field names to their values'
            return OrderedDict(zip(self._fields, self)) 
    
        def _replace(_self, **kwds):
            'Return a new Arc object replacing specified fields with new values'
            result = _self._make(map(kwds.pop, ('head', 'tail'), _self))
            if kwds:
                raise ValueError('Got unexpected field names: %r' % kwds.keys())
            return result 
    
        def __getnewargs__(self):
            'Return self as a plain tuple.  Used by copy and pickle.'
            return tuple(self) 
    
        head = _property(_itemgetter(0), doc='Alias for field number 0')
        tail = _property(_itemgetter(1), doc='Alias for field number 1')
    

    You can cut, paste, and modify this code, or just subclass from it as shown in the namedtuple docs.

    To extend this class, build off of the fields in Arc:

    WeightedArc = namedtuple('WeightedArc', Arc._fields + ('weight',))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class A with its inner class defined A1 and class B with
I have class which has a method that needs to return three DataTables. I
I have class that applies a dotted style border property to a text block
I have class(Customer) which holds more than 200 string variables as property. I'm using
I have class that has a property that is of type string[]. I need
I have class Foo. Foo has a property of public string x. I would
I have class deriving from TextBox to which I attach a dependency property of
I have class methods like these in a message model: def delete_all_users_messages(user_id, parent_id) message
I have class method that returns a list of employees that I can iterate

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.