In subclass ComputerGame, Toy.__init__(self,n), n is undefined.
Why is it warning me that the variable n is undefined when I try to call the __init__() function in the base class? Simply put, why is Toy.__init__(self,n) not callable in the subclass? I have declared ComputerGame (Toy):. Why isn't it inheriting the method Toy.__init__(self,n)?
Should I declare the variable outside the class?
class Toy:
def __init__(self,n):
self.__ToyName = n
self.__ToyID = ""
self.__Price = 0.00
self.__MinimumAge = 4
def SetToyName(self,p):
self.__ToyName = p
def SetToyID(self,d):
self.__ToyID = d
def SetPrice(self,i):
self.__Price = i
def SetMinimumAge(self,i):
self.__MinimumAge = i
def GetToyName(self):
return (self.__ToyName)
def GetToyID(self):
return (self.__ToyID)
def GetPrice(self):
return (self.__Price)
def GetMinimumAge(self):
return (self.__MinimumAge)
class ComputerGame(Toy):
def __init__(self):
Toy.__init__(self,n)
self.__Catogory = ""
self.__Console = ""
def SetCatogory(self,c):
self.__Catogory = c
def SetConsole(self,c):
self.__Console = c
def GetCatogory(self):
return (self.__Catogory)
def GetConsole(self):
return (self.__Console)
This is a scoping issue:
class ComputerGame(Toy):
def __init__(self):
Toy.__init__(self,n)
This is the first appearance of n. When you create a ComputerGame, your __init__ methods requires you (the calling program) to supply n as part of the creation. That's how you wrote the code.
The __init__ above assumes that n will appear magically from nowhere. I'm unclear what data flow you expect from this -- and so is the Python compiler. I don't know how to fix this for you, since you failed to supply a Minimal, complete, verifiable example. We should have at least one line of main code to create the object.
One strong possibility is fairly simple:
class ComputerGame(Toy):
def __init__(self, n):
Toy.__init__(self, n)
NewFPS = ComputerGame("Universal Uzi")
Does that clear it up?
Related
I have a simple class. I want to set the periodicity property using a private attribute which is tested before being used to set the public attribute. I am passing self around the class module, when I run the test code the function seems to expect 2 arguments. I thought it should ignore self and ask for the one freq parameter. I am trying to be "pythonic" and use #property with .setter. Do I need to change the property call? If I remove self from set_periodicity it says it does not recognize self.
class ModelSetUp:
def __init__(self):
self.periodicity= None
#property
def set_periodicity(self,freq):
self.periodicity = self._periodicity
#set_periodicity.setter
def set_periodicity(self,freq):
if freq in ("Q","S","A"):
self._periodicity = freq
else:
raise ValueError("Frequency Must Be Q, S or A")
if __name__=="__main__":
mod = ModelSetUp()
mod.set_periodicity("Q")
Properties don't take any arguments, nor are they callable. Try this:
class ModelSetUp:
def __init__(self):
self._periodicity = None
#property
def periodicity(self):
return self._periodicity
#periodicity.setter
def periodicity(self, freq):
if freq in ("Q","S","A"):
self._periodicity = freq
else:
raise ValueError("Frequency Must Be Q, S or A")
if __name__=="__main__":
mod = ModelSetUp()
mod.periodicity = "Q"
I'm trying to configure a decorator at run time. This is somewhat related to my earlier question: How to configure a decorator in Python
The motivation for this is that I'm trying to use the Thespian troupe code "as-is".
Is it legal to have this code here, where I've defined the class (and therefore called the decorator) inside a class method? Again, the reason for this is that I could feed the max_count argument prior to the decorator being call.
The module is calculator.calculator (yes, bad choice perhaps)
class Scheduler:
def __init__(self):
self.actor_system = None
def start(self):
self.actor_system = ActorSystem('multiprocTCPBase')
def stop(self):
self.actor_system.shutdown()
def launch(self, count, func_and_data, status_cb):
class CalcPayload:
def __init__(self, func_and_data, status_cb):
self.func_and_data = func_and_data
self.status_cb = status_cb
#troupe(max_count=count)
class Execute(ActorTypeDispatcher):
def receiveMsg_CalcPayload(self, msg, sender):
func = msg.func_and_data['func']
data = msg.func_and_data['data']
status_cb = msg.status_cb
self.send(sender, func(data, status_cb))
exec_actor = self.actor_system.createActor(Execute)
for index in range(len(func_and_data)):
calc_config = CalcPayload(func_and_data[index], status_cb)
self.actor_system.tell(exec_actor, calc_config)
for index in range(len(func_and_data)):
result = self.actor_system.listen(timeout)
self.actor_system.tell(exec_actor, ActorExitRequest())
For various reasons, I can't apply the decorator to the class when I use it. There is a brief discussion on this in the question I referenced.
While not invalid, it is generally inadvisable to define a class as a local variable inside a function, as it would make access to the class difficult outside the function.
Instead, you can define the classes outside the function, and apply the decorator function to the class when it's actually needed by calling the decorator function with the class object:
class CalcPayload:
def __init__(self, func_and_data, status_cb):
self.func_and_data = func_and_data
self.status_cb = status_cb
class Execute(ActorTypeDispatcher):
def receiveMsg_CalcPayload(self, msg, sender):
func = msg.func_and_data['func']
data = msg.func_and_data['data']
status_cb = msg.status_cb
self.send(sender, func(data, status_cb))
class Scheduler:
def __init__(self):
self.actor_system = None
def start(self):
self.actor_system = ActorSystem('multiprocTCPBase')
def stop(self):
self.actor_system.shutdown()
def launch(self, count, func_and_data, status_cb):
exec_actor = self.actor_system.createActor(troupe(max_count=count)(Execute))
for index in range(len(func_and_data)):
calc_config = CalcPayload(func_and_data[index], status_cb)
self.actor_system.tell(exec_actor, calc_config)
for index in range(len(func_and_data)):
result = self.actor_system.listen(timeout)
self.actor_system.tell(exec_actor, ActorExitRequest())
The actor_system is going to want to build instances of your class. That means it needs to be able to derive the class object- you cannot define it inside of a method.
If you really need to apply the decorator separately, you maybe could do
def launch(self, count, func_and_data, status_cb):
wrapped = troupe(max_count=count)(Executor)
exec_actor = self.actor_system.createActor(wrapped)
So I'm reading through An Introduction to Programming Using Python and doing the exercises for my own edification. I'm learning about custom classes and get to a part where it states: "The program should use a class named Quizzes that has an instance variable to hold a list of the six grades..." So I tried the following:
class Quizzes:
def __init__(self, grade1=0):
self._grade1 = grade1
def setGrade1(self, grade1):
self._grade1 = grade1
def getGrade1(self):
return self._grade1
def grades(self):
return []
def gradeInsert(self, a=grades()):
a.append(self._grade1)
In this case, I get "TypeError: grades() missing 1 required positional argument: 'self'.
I've also tried:
def __init__(self, grade1=0, grades=[]):
self._grade1 = grade1
self._grades = grades
def setGrade1(self):
self._grade1 = grade1
def setGrades(self):
self._grades = [self.getGrade1()]
def getGrades(self):
return self._grades
But I get a blank [ ]. Any modification I do to grades=[ ] in _ init _ reflects in my getGrades call. So how do I get setGrades to actually set? Or am I totally off the mark?
Instance variables should be initialized inside of __init__
class Quizzes:
def __init__(self):
self._grades = [] # Here, we initialize the list
def grades(self):
return self._grades # We can return the instance variable here
def gradeInsert(self, a): # No reason for a default argument here
self._grades.append(a) # Add the value to the list
In your first example you try to call grades() which is a member function of the Quizzes class. Member functions expect the instance as the first argument, either before the dot (the_instance.grades()) or as (less common) as an explicit argument (grades(the_instance)). You provide neither and thus Python complains.
In your second example it is not clear what you actually do after the class is defined.
In general, the two classes do not have much to do with the exercise, which would be solved by the following:
class quizzes(object):
def __init__(self, grades = None):
if grades is None:
self.grades = [0]*6
else:
self.grades = grades[:6]
BTW: it is not recommended to use a mutable object as default argument as this object is shared between all invocations.
Instead of using a global variable, I'm trying to make an instance of a variable in a class, as it seems to be best practice. How do I reference this variable across other functions in the class? I would have thought that Test.running_sum would work or at least running_sum in test_function, but I'm not having any luck with either. Thanks very much!
class Test:
def __init__(self):
self.root = None
running_sum = 0
def test_function(self):
print(Test.running_sum)
return
x = Test()
x.test_function()
Error:
Traceback (most recent call last):
File "so.py", line 1, in <module>
class Test:
File "so.py", line 10, in Test
x = Test()
NameError: name 'Test' is not defined
Use self parameter provided in the method signature.
Note that what you wrote is not a method, but an external function using class Test. To write a method of Test, the def should be at one level of indentation inside class Test as following:
class Test:
def __init__(self):
self.running_sum = 0
def test_function(self):
print(self.running_sum)
There are several things to add if you want an explanation behind this "best practice".
Assuming you write the following code:
class Test:
numbers = []
def add(self, value):
self.numbers.append(value)
The Test.numbers list is instantiated once and shared accross all instances of Test. Therefore, if 2 different instances add to the list, both act on the same list:
a = Test()
b = Test()
a.add(5)
b.add(10)
assert a.numbers == b.numbers == Test.numbers
When creating instance variables in the __init__ function, __init__ will be run at each instantiation, and therefore, the list will no longer be shared because they will be created for each individual instances.
class Test:
def __init__(self):
self.numbers = []
def add(self, number):
self.numbers.append(number)
a = Test()
b = Test()
a.add(5)
b.add(10)
assert a != b
As an object attribute: each object gets its own.
Test is the class; self is the Test object that invoked the method.
class Test:
def __init__(self):
self.root = None
self.running_sum = 0
def test_function(self):
self.running_sum += 1
print(self.running_sum)
return
x = Test()
y = Test()
x.test_function()
y.test_function()
Output:
1
1
As a class attribute: all objects share the same variable.
self.__class__ is the class of the invoking object (i.e. Test).
class Test:
running_sum = 0
def __init__(self):
self.root = None
def test_function(self):
self.__class__.running_sum += 1
print(self.__class__.running_sum)
return
x = Test()
y = Test()
x.test_function()
y.test_function()
Output:
1
2
how do I reference this variable across other functions in the class
Several things I see wrong here. First of all, you are calling running_sum on the class itself which doesn't make sense since you are declaring running_sum as an attribute of an instance of Test. Second, from the way you formatted your question, it seems that test_function is outside of the class Test which doesn't make sense since you are passing self to it, implying it is an instance method. To resolve you could do this:
class Test:
def __init__(self):
self.running_sum = 0
def test_function(self):
print(self.running_sum)
Then again this also is weird... Why would you need a "test_function" when you can simply test the value of running_sum by simply doing:
x = Test()
x.running_sum
In your __init__ function, you've created a local variable. That variable will no longer exist after the function has completed.
If you want to create a variable specific to the object x then you should create a self.running_sum variable
class Test:
def __init__(self):
self.root = None
self.running_sum = 0
def test_function(self):
print(self.running_sum)
If you want to create a variable specific to the class Test then you should create a Test.running_sum variable.
class Test:
running_sum = 0
def __init__(self):
self.root = None
def test_function(self):
print(Test.running_sum)
A derived class has access to its base class member functions implicitly, unless I am mistaken. A derived class can also access its base class' attributes by prefixing a call to them like this: BaseClass.base_attribute. But I seemingly do not understand how instances of a derived class can use the methods of the base class. Example:
class Visitor():
""" Interface to Visitor
provide an interface to visitors that
perform an operation on a data collection """
def visitProduce():
pass
def visitMeat():
pass
def visitBakedGoods():
pass
def visitDairy():
pass
def visitNonFood():
pass
class PriceVisitor(Visitor):
__cost = 0.0 # total cost of groceries
def __init__(self):
self.__cost = 0.0
def visitProduce(self, p):
self.__cost += p.price()
def visitMeat(self, m):
self.__cost += m.price()
def visitBakedGoods(self, b):
self.__cost += b.price()
def visitDairy(self, d):
self.__cost += d.price()
def visitNonFood(self, nf):
self.__cost += nf.price()
class Groceries():
shopping_cart = [] # list of grocery items
def Groceries(self):
self.shopping_cart = []
def addProduce(self, p):
pass
def addMeat(self, m, lb):
pass
def addBakedGoods(self, b):
pass
def addDairy(self, d):
pass
def addNonFood(self, nf):
pass
def accept(self, v):
pass
def getShoppingCart(self):
print(self.shopping_cart)
def calculateCost(self, v):
for item in self.shopping_cart:
item.accept(v)
item.details()
print('Total cost is: $', v.__cost)
class Produce(Groceries):
def addProduce(self):
Groceries.shopping_cart.append(self)
def accept(self, v):
v.visitProduce(self)
def price(self):
return self.__price
def details(self):
print(self.__name, ' for: $', self.__price + '')
class Apples(Produce):
__name = None
__price = 3.25
def __init__(self, name):
self.__name = name
And here is a test of the Apple, Produce, Groceries, and PriceVisitor classes
import VisitorPattern as vp
def main():
# Visitor object
my_visitor = vp.PriceVisitor()
# Grocery object stores objects in its shopping_cart attribute
my_groceries = vp.Groceries()
# Add items
red_apple = vp.Apples('red apple')
gold_apple = vp.Apples('gold apple')
red_apple.addProduce()
gold_apple.addProduce()
my_groceries.getShoppingCart()
my_groceries.calculateCost(my_visitor)
if __name__ == '__main__':
main()
Now, the way I understand it is that upon the construction of the instance of Apple, it has access to Produce's method price(). Calling this method with an instance of the Apple class will then pass its own instance in place of the 'self'. The program then returns the value of the __price attribute belonging to the instance calling the method, in this case Apple. However, I get this error:
C:\Users\josep_000\Documents\School\Summer 2015\Python Assignment 4>python test.
py
[<VisitorPattern.Apples object at 0x026E0830>, <VisitorPattern.Apples object at
0x026E0910>]
Traceback (most recent call last):
File "test.py", line 23, in <module>
main()
File "test.py", line 20, in main
my_groceries.calculateCost(my_visitor)
File "C:\Users\josep_000\Documents\School\Summer 2015\Python Assignment 4\Visi
torPattern.py", line 60, in calculateCost
item.accept(v)
File "C:\Users\josep_000\Documents\School\Summer 2015\Python Assignment 4\Visi
torPattern.py", line 71, in accept
v.visitProduce(self)
File "C:\Users\josep_000\Documents\School\Summer 2015\Python Assignment 4\Visi
torPattern.py", line 28, in visitProduce
self.__cost += p.price()
File "C:\Users\josep_000\Documents\School\Summer 2015\Python Assignment 4\Visi
torPattern.py", line 74, in price
return self.__price
AttributeError: 'Apples' object has no attribute '_Produce__price'
How does the binding and namespaces actually work in inheritance? I could just write the price() method in each of Produce's derived classes, but that would defeat the point of inheritance. I think my problem also stems from name mangling, but still don't know what happens if I don't make my attributes 'private'. Clarification would be great. Thanks
Edit
I declared the constructor of Groceries wrong:
# Wrong way
def Groceries(self):
self.shopping_cart = []
# Should be
def __init__(self):
self.__shopping_cart = []
The product of a full time job and homework in the evening
What is the order of namespaces in inheritance?
Python uses the Method Resolution Order to find the method bound to that instance of the object.
It also invokes name mangling, which is why you can't find the method, _Produce__price. You're trying to use .__price but when it is inherited, Python adds the name of the class to the front of the name. Don't use two underscores, change the two underscores to one, and your code will work as you expect, and you'll consistently look up ._price which won't invoke the name mangling.
See the docs for more on this:
https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references
Not really a direct answer to all your questions but I hope the following code sheds some light on how to do inheritance in Python.
class Produce(object):
def __init__(self, name=None, price=None):
self.__name = name
self.__price = price
def __str__(self):
return self.__name
#property
def bulk_price(self):
return self.__price * 100
class Apple(Produce):
def __init__(self, name="Apple"):
self.__name = name
self.__price = 3.25
super(self.__class__, self).__init__(self.__name, self.__price)
a = Apple("Gold Apple")
print a
print a.bulk_price
# Gold Apple
# 325.0
As you can see, I made name and price inaccessible in both classes. This way, I cannot just call them explicitly, i.e. a.__price. By using super as well in the child class, I am able to avoid referring to the base class further while still having access to its methods.
I have saw your error, your parent need to call child's function, but you have not transferred child to parent, so it will get the errors.Now I give my example:
class A:
def __init__(self, handler):
self.a = 5
self.real_handler = handler
def get(self):
print "value a = %d"%self.a
self.real_handler.put()
class B(A):
def __init__(self):
A.__init__(self, self) ##transport B to A
self.b = 3
def get(self):
print "value b is %d"%self.b
A.get(self)
def put(self):
self.b = 6
print "value b change into %d"%self.b
if __name__=="__main__":
b = B()
b.get()
In parent B, it will call the child A's fuction put(). I hope this can help you.