1. 5 Cups of Ubuntu
Join Date
Nov 2008
Beans
37

## Python Help

I'm new to using classes in Python, really Python in general, and I'm not sure what I'm doing wrong with this. I'm hoping someone can easily tell me how to fix this.

Code:
```class Cp:
x = x/1000
def O2(self, x):
self.state = 30.03235 + 8.772972*x + (-3.988133*x**2) + (0.788313*x**3) - 0.741599/(x**2)
def N2(self, x):
self.state = 19.50583 + 19.88705*x + (-8.598535*x**2) + (1.369784*x**3) + 0.527601/(x**2)
print(Cp.O2(1500))```

2. ## Re: Python Help

Originally Posted by Elfo33
I'm new to using classes in Python, really Python in general, and I'm not sure what I'm doing wrong with this. I'm hoping someone can easily tell me how to fix this.

Code:
```class Cp:
x = x/1000
def O2(self, x):
self.state = 30.03235 + 8.772972*x + (-3.988133*x**2) + (0.788313*x**3) - 0.741599/(x**2)
def N2(self, x):
self.state = 19.50583 + 19.88705*x + (-8.598535*x**2) + (1.369784*x**3) + 0.527601/(x**2)
print(Cp.O2(1500))```
You are calling the method on the class itself and not an instance of the class. Try

Code:
```c = Cp()
print(c.O2(1500))```
EDIT: I didn't look carefully enough, there are more problems: the functions O2 and N2 should return a value if you want to print something.
Last edited by Erik1984; November 17th, 2012 at 04:06 PM.

3. ## Re: Python Help

It should work this way:
Code:
```Class Cp:
def O2(self, x):
self.state = 30.03235 + 8.772972*x + (-3.988133*x**2) + (0.788313*x**3) \$
return self.state
def N2(self, x):
self.state = 19.50583 + 19.88705*x + (-8.598535*x**2) + (1.369784*x**3) \$
return self.state

c = Cp()
print(c.O2(1500))```
You didn't use the x defined inside the class so I removed it. The line x = x/1000 also presented an error as x wasn't defined yet.

4. 5 Cups of Ubuntu
Join Date
Nov 2008
Beans
37

## Re: Python Help

Originally Posted by Euroman
It should work this way:
Code:
```Class Cp:
def O2(self, x):
self.state = 30.03235 + 8.772972*x + (-3.988133*x**2) + (0.788313*x**3) \$
return self.state
def N2(self, x):
self.state = 19.50583 + 19.88705*x + (-8.598535*x**2) + (1.369784*x**3) \$
return self.state

c = Cp()
print(c.O2(1500))```
You didn't use the x defined inside the class so I removed it. The line x = x/1000 also presented an error as x wasn't defined yet.
Thanks for looking at this for me. However, I need to incorporate the x/1000 somehow. The idea is that x is a temperature, and for this equation to work, the given temperature (in K) is divided by 1000. I have a bunch of different elements that I need to incorporate into enthalpy and entropy classes/functions as well, so the idea was that I could create something that would give me a value for h.O2(T) vs h.N2(T). Additionally, I'll be using s.O2(T) in an fsolve to find what the temperature is.

Is there a better way to structure this?

5. ## Re: Python Help

you are probably better of requiring a units library if you are concerned that users put in the wrong unit.
e.g. https://bitbucket.org/kiv/unum

6. ## Re: Python Help

Moved to Programming Talk.

7. ## Re: Python Help

what i don't get is what do you you need that class for? what i see is just a bunch of functions that could be standalone.

if you need to get that /1000 you could simply go with
Code:
```def O2( _x ):
x = _x/1000.0
return 30.03235 + 8.772972*x + (-3.988133*x**2) + (0.788313*x**3)

print( O2(1500) )```
Last edited by Vaphell; November 17th, 2012 at 06:56 PM.

8. ## Re: Python Help

Originally Posted by Vaphell
what i don't get is what do you you need that class for? what i see is just a bunch of functions that could be standalone.
[/code]
My thoughts exactly. If you don't keep data in class instances then using a class isn't bringing you much. There are languages (like Java) where everything is a class and you can't have stand-alone functions, so you define them as "static" functions in some utility class, but with Python you don't need this.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•