Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__init__ function arguments
#1
Hello,

I am trying to make an __init__ function's argument be the previous argument of that function divided by 2.

class item: def __init__(self,value1,value2): self.v1 = value1 self.v2 = value2 x = item(10,item.v1/2)
However, when I run it I get the message that "type object 'item' has no attribute v1".

Any other ways to do it?
Reply
#2
the statement:
x = item(10,item.v1/2)
is outside of the class, so cannot see v1 or v2

this script is not worthy of a class, but:
better to use inside of a function like:
class item: def __init__(self,value1,value2): self.v1 = value1 self.v2 = value2 def testit(): myitem = item(45, 78) ... # some time later x = item(10, myitem.v1/2) if __name__ == '__main__': testit()
Reply
#3
Maybe a little confusing to instantiate both myitem and x Larz60+,as it not clear that both is needed.

Here the basic.
class Item: def __init__(self, value1, value2): self.v1 = value1 self.v2 = value2
Use class:
>>> # Instantiate the Item object >>> x = Item(8, 4) >>> # Access the instance attributes >>> x.v1 8 >>> x.v2 4 >>> # At this point you can what you want instance attributes >>> x.v1 + x.v2 / 2 10.0 >>> x.v1 + x.v2 * 20 / x.v1 18.0
Reply
#4
(Jan-18-2019, 01:58 PM)Larz60+ Wrote: the statement:
x = item(10,item.v1/2)
is outside of the class, so cannot see v1 or v2

this script is not worthy of a class, but:
better to use inside of a function like:
class item: def __init__(self,value1,value2): self.v1 = value1 self.v2 = value2 def testit(): myitem = item(45, 78) ... # some time later x = item(10, myitem.v1/2) if __name__ == '__main__': testit()

Larz60+ thank you so much, but I just realized I made a mistake in the example!! I meant
x = item(10, x.v1/2)
So sorry for this, I really appreciate the reply from both you and snippsat!
Reply
#5
You cannot reference x.v1 as an argument to the __init__() method because x doesn't exist yet. The class and its attributes aren't instantiated until after the __init__() method has completed. Until then, the attributes cannot be accessed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Two arguments in input function Alfredd 4 737 Nov-09-2025, 12:56 AM
Last Post: Pedroski55
  function arguments Curbie 2 1,010 Apr-30-2025, 05:23 PM
Last Post: Curbie
  calling external function with arguments Wimpy_Wellington 6 3,885 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  'namespace' shorthand for function arguments? shadowphile 5 4,417 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 3,604 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  How to deal with self and __init__ in function MrFloyd_telomerase 1 2,727 Apr-26-2021, 08:15 PM
Last Post: buran
  Possible to dynamically pass arguments to a function? grimm1111 2 3,581 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  How to pass multiple arguments into function Mekala 4 4,148 Jul-11-2020, 07:03 AM
Last Post: Mekala
  How to give a name to function arguments in C-API? WonszZeczny 0 2,140 Jun-22-2020, 10:20 AM
Last Post: WonszZeczny
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 3,465 Apr-06-2020, 09:14 AM
Last Post: bowlofred

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.