Add a method to the int class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barberomarcelo@gmail.com

    #1

    Add a method to the int class

    How can I add a method to the int class?

  • placid

    #2
    Re: Add a method to the int class


    barberomarcelo@ gmail.com wrote:
    How can I add a method to the int class?
    sub class it
    >>class MyInt(int):
    >> def times(self,mult iple):
    >> return self * multiple
    >>>
    >> a = 2
    >> print a
    2
    >> a = MyInt(2)
    >> print a
    >> 2
    >> print a.times(2)
    4


    --Cheers

    Comment

    • Terry Reedy

      #3
      Re: Add a method to the int class


      <barberomarcelo @gmail.comwrote in message
      news:1152505455 .432235.275880@ h48g2000cwc.goo glegroups.com.. .
      How can I add a method to the int class?
      You can't. The closest is to subclass int and add your method.



      Comment

      Working...