Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does numpy do this?
#6
oh ok, it was simpler than that sorry. That is what I wanted to do. I just overthought it

class Foo(object):
    
    def __init__(self, someVar):
        self._x = someVar
    
    def asList(self):
        return [self._x]
        
    def asString(self):
        return "'{}'".format(self._x)
     
    def __repr__(self):
        return "{}".format(self._x)
    
foo = Foo(5)    # Result: 5
foo.asList()    # Result: [5] # 
foo.asString()  # Result: '5' # 
I don't know why I thought I would be able to have the constructor return an integer, and then "magically" have methods of that object acting on the integer with a "." operator. It makes absolutly no sense. Never mind. Minor lapsus.
Reply


Messages In This Thread
How does numpy do this? - by rudihammad - Jun-19-2020, 06:08 AM
RE: How does numpy do this? - by ndc85430 - Jun-19-2020, 06:14 AM
RE: How does numpy do this? - by rudihammad - Jun-19-2020, 06:41 AM
RE: How does numpy do this? - by buran - Jun-19-2020, 06:43 AM
RE: How does numpy do this? - by buran - Jun-19-2020, 06:44 AM
RE: How does numpy do this? - by rudihammad - Jun-19-2020, 07:17 AM

Forum Jump:

User Panel Messages

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