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

  • Home
  • SEARCH
  • 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 8308679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:52:47+00:00 2026-06-08T18:52:47+00:00

I need a small help with slots . class bstream(object): __slots__ = [‘stream’] stream

  • 0

I need a small help with slots.

class bstream(object):
  __slots__ = ['stream']
  stream = string()

  def __new__(self, stream, encoding=None):
    if encoding == None:
      encoding = ENCODING['default']
    if isinstance(stream, bytes):
      self.stream = stream.decode(encoding)
    elif isinstance(stream, string):
      self.stream = stream
    else: # if unknown type
      strtype = type(stream).__name__
      raise(TypeError('stream must be bytes or string, not %s' % strtype))
    return(self)

  def __repr__(self):
    '''bstream.__repr__() <==> repr(bstream)'''
    chars = ['\\x%s' % ('%02x' % ord(char)).upper() for char in self.stream]
    result = ''.join(chars)
    return(result)

  def func(self):
    return(1)

Don’t be confused with those string type and ENCODINGS dictionary: they are constants.
The problem is that the following commands don’t work as I expect:

>>> var = bstream('data')
>>> repr(var)
<class '__main__.bstream'> # Instead of '\\x64\\x61\\x74\\x61'
>>> var.func()
TypeError: unbound method func() must be called with bstream instance as first argument (got nothing instead)

What’s wrong? I’d really like to leave my class immutable, so solutions with the removing of slots are really not very good ones. 🙂 Thanks a lot!

  • 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-08T18:52:49+00:00Added an answer on June 8, 2026 at 6:52 pm

    You want to use __init__, not __new__.

    __new__ is a class method for which the first argument (self) is the class object, not the newly created object. It has to return the new object. You usually don’t want to redefine it, but you can if you want to do things like return an existing object.

    __init__ is a regular instance method, and the first argument (self) is the newly created instance. It works like constructors in other languages.

    To fix this, change the method name to __init__ and delete the final line (return(self)). __init__. must always return None; returning anything else results in a TypeError.

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

Sidebar

Related Questions

I need small help, I'm using class Qslider from qt, how can I get
I need small help. I have 2 lists( say A and B) which have
need a small basic help, I am creating a XML file on the fly
I need some help with a small php problem. But i dont know how
I need some help adding conditions to a linq query. Take this small example
I need help with some code that i am writing for a small text
I need help concerning following code: http://jsfiddle.net/8PsdE/ <SELECT ID=pizzasize NAME=pizzasize> <OPTION VALUE=s>small <OPTION VALUE=m>medium
I'm trying to make a small program more robust and I need some help
I need a small help regarding the spinner,when i click any part of the
I need some help determining the frequencies of these small peaks. I know it

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.