Reputation: 941
object-oriented programming is pretty new to me. I am just trying to learn a bit about classes and methods. I am trying to write a simple class called 'Object' and then define a few geometrical objects e.g. circle, rectangle etc. I then want to have attributes such as 'area' and 'circumference' where I would like to access the area of a circle as:
class object:
import numpy as np
pi = np.pi
def circle(self):
def area(self, radius):
self.area=self.radius**2 * self.pi
s = object()
print(s.__dict__)
print(s.circle.area(5))
Upon run, I am getting:
{}
Traceback (most recent call last):
File "/Users/aya/Desktop/test.py", line 12, in <module>
print(s.circle.area(5))
AttributeError: 'function' object has no attribute 'area'
How do I make s.circle.area(5)
work?
Upvotes: 0
Views: 642
Reputation: 374
Everything isn't fresh for me, but what are you trying to do here s.circle.area(5)
is accesssing to an attribute of Object, who should also be an object beceause you try to call a method to this specific attribute.
In clear, '.' allow you to only access to an attribute or a function. For exemple
class Square:
def __init__(self, size):
self.size = size
def resize(self, new_size):
self.size = new_size
#Usage
>>> form = Square(5)
>>> print(form.size)
>>> 5 #output
>>> form.resize(120)
>>> print(form.size)
>>> 120 #output
In your code, it's really weird and don't make any sense, many confusion. You are defining a function area inside a function circle, which try to create an area attribute using the Object inexisting self.radius with an existing attribute pi.
Imaginating something like that could work, you're trying to access the function of the function, by calling a method of circle (no sense, hard to explain).
Still in our square, you access an attribute :
>>> form.size
Now you try to call a function from it :
>>> form.size.action()
In this case, you're using an integer method (int are also class containing a method)
By calling an object method :
>>> form.reshape(8) #changing the current size
>>> form.reshape(8).action()
It's calling in this way a method on the reshape's function return. It seem that's you're not really confident with function definition (two def keyword following each other, it's possible, but not sure that's your goal), import I'm not sure if it have been choose really wisely (numpy will be imported at each object creation, usefull for you ?). Be careful with basics, more you're basic will be stronger, more you will have understand what you're doing, and in programmation, you must know what you're doing.
How do I make s.circle.area(5) work?
At least, you can do
import numpy
class Circle:
def __init__(self, area = 0):
self.area = area
def area(self, new_area):
self.area = new_area
return self.area
class Object:
pi = numpy.pi
circle = Circle()
>>> obj = Object()
>>> obj.circle.area(5)
I finally can do this action ! Many things aren't good practice, or won't have the desired behavior if it's not used wisely, you have to understand what's happening. I understand that's you're learning, and it's really nice, but don't run (and not really fast) without knowing a bit to walk. (I perhaps judge a bit to much sorry, it's for you, be careful)
P.S. : Everything I spoke about here have plenty of documentation. Class definition is really a standart in Python. Object oriented programming without creating object, it could be really paradoxal. Here some documentation : https://docs.python.org/3.7/tutorial/classes.html#a-first-look-at-classes
Upvotes: 0
Reputation: 413
Below is the example which will help you
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# Instantiate the Dog object
philo = Dog("Philo", 5)
mikey = Dog("Mikey", 6)
# Access the instance attributes
print("{} is {} and {} is {}.".format(
philo.name, philo.age, mikey.name, mikey.age))
# Is Philo a mammal?
if philo.species == "mammal":
print("{0} is a {1}!".format(philo.name, philo.species))
when i run above file i got.
Philo is 5 and Mikey is 6.
Philo is a mammal!
Inner Class Example
#!/usr/bin/env python
class Human:
def __init__(self):
self.name = 'Guido'
self.head = self.Head()
self.brain = self.Brain()
class Head:
def talk(self):
return 'talking...'
class Brain:
def think(self):
return 'thinking...'
if __name__ == '__main__':
guido = Human()
print guido.name
print guido.head.talk()
print guido.brain.think()
Upvotes: 1
Reputation: 1611
1) The name object
is already taken as a basic Python object, you should use another name (Polygon
?)
2) I think you meant for circle
to be a new class, not a function. You can make a class be similiar to, or "inherit from", another class by including it in the class declaration:
class Circle (Polygon): #NOT object, see number one
def __init__ (self, radius): #called to make a new Circle
self.radius = radius #give this circle its radius
def area (self):
# self.pi is already declared because Polygon states it, and Circles "inherit" that trait
return self.pi * (self.radius ** 2)
Now, to make a circle:
circle = Circle (5) # or Circle (radius = 5)
pi = circle.pi
# or Circle.pi, since it belongs to the whole class (upper-case) and not just the object (lower-case)
area = circle.area() #needs to be on a specific circle, because radii can vary
Upvotes: 0