473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sending a class as an argument

Hi,

Our class has its attributes set as classes, as in

MyClass.Phone.Value='34562346'
MyClass.Phone.Private=True

Inside the MyClass definition we have a function like this:

def MyFunc(self,clsProperty):
if clsProperty.Private:
print 'Private property'
else:
print ClsProperty.Value

In our code, we then call
>>MyClass.MyFunc(MyClass.Phone)
We want to be able in our code instead simply to call:
>>MyClass.MyFunc(Phone) or MyClass.MyFunc('Phone')
But we can't get it to work. If we rewrite the function as follows:

def MyFunc(self,attr):
if self.attr.Private:
print 'Private'
else:
print self.attr.Value

we get an error.

Is there a better way to do this? Thanks

Jan 29 '07 #1
2 1152
On Sun, 28 Jan 2007 20:24:23 -0800, manstey wrote:
Hi,

Our class has its attributes set as classes, as in

MyClass.Phone.Value='34562346'
MyClass.Phone.Private=True
The Python convention is that classes have initial capitals (MyClass),
instances do not, and nor do attribute names.

I'm thinking you're doing this:

class Phone: pass
class MyClass: pass

# now set a _class_ attribute -- all instances share this attribute
MyClass.Phone = Phone() # attribute name is the same as a class

Why do all instances of MyClass share the same phone number?

I think that's a bad design. You should probably do this:

# now set an _instance_ attribute
instance = MyClass()
instance.phone = Phone()

Of course, in practice you'd do this in an __init__ method rather than as
two separate lines:

instance = MyClass() # automatically creates instance.phone
Of course, I could be wrong -- you haven't really given enough information
for me to be sure what you're doing.

Inside the MyClass definition we have a function like this:
The word is "method", not function. Methods live in classes, functions
outside.

def MyFunc(self,clsProperty):
if clsProperty.Private:
print 'Private property'
else:
print ClsProperty.Value
If you have a whole lot of attributes with the same attribute (in this
case, private), you should really be thinking about a more object-oriented
design. As it stands now, your MyClass needs to know all the fundamental
workings of your Phone class, your Name class, your Address class, etc.
That's bad design. Your MyClass should just say "give me your value" and
the Phone class should know that if it is private, it returns "private
property". Etc.
In our code, we then call
>>>MyClass.MyFunc(MyClass.Phone)

We want to be able in our code instead simply to call:
>>>MyClass.MyFunc(Phone) or MyClass.MyFunc('Phone')

But we can't get it to work. If we rewrite the function as follows:

def MyFunc(self,attr):
if self.attr.Private:
print 'Private'
else:
print self.attr.Value

we get an error.
Oooh, no don't tell us what it is, let me guess! I love guessing games!

Does it crash your PC?

Here's how I would do it. Each attribute should know how to "MyFunc"
itself: give it a myfunc method.

class Phone:
def __init__(self, number, private=False):
self.phonenumber = number
self.private = private
def myfunc(self):
if self.private:
return "private"
else:
return str(self.phonenumber)
Now you can simplify your MyFunc method:

def myfunc(self, attrname):
"""Delegate myfunc to the named attribute."""
return getattr(self, attrname).myfunc()

Or not even bother, and just write MyClass.phone.myfunc().

--
Steven D'Aprano
Jan 29 '07 #2
En Mon, 29 Jan 2007 01:24:23 -0300, manstey <ma*****@csu.edu.auescribió:
>
Our class has its attributes set as classes, as in

MyClass.Phone.Value='34562346'
MyClass.Phone.Private=True

Inside the MyClass definition we have a function like this:

def MyFunc(self,clsProperty):
if clsProperty.Private:
print 'Private property'
else:
print ClsProperty.Value
This method does not use `self` at all, and that's rather suspicious for
an instance method.
And you set properties on the class itself? So you never create instances
of that class?
In our code, we then call
>>>MyClass.MyFunc(MyClass.Phone)

We want to be able in our code instead simply to call:
>>>MyClass.MyFunc(Phone) or MyClass.MyFunc('Phone')

But we can't get it to work. If we rewrite the function as follows:

def MyFunc(self,attr):
if self.attr.Private:
print 'Private'
else:
print self.attr.Value

we get an error.
Surely an AttributeError, because you dont have any attribute named "attr".
Notice that on this version you are using `self`, but on the above version
you didnt use it.
There are easy ways in Python to access an attribute by name, but I think
that this would just add more noise to your code. Please tell us about
your design, or rethink it. Do actually exist instances of MyClass, or
not? I can imagine "Phone" being an attribute of a certain instance of
Person, by example, but not a class attribute.
Is there a better way to do this? Thanks
Surely, but we need to know your goal, what do you really want to do, not
how do you think you should do that.

--
Gabriel Genellina

Jan 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
2
by: Chuck Marques | last post by:
I'm trying to implement sending an object through messaging, but I receive an error stating I can't deserialize the object. Any clues as to why. I have the following: <Serializable()> Public...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
1
by: Nick Gilbert | last post by:
Hi, I'm trying to create a system whereby my desktop application submits it's order to an online server using a webservice (pretty standard!). So, I added a new project to my solution to...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
9
by: 7stud | last post by:
Hi, I'm trying to figure out what this passage from GvR's tutorial means: ------- Class definitions, like function definitions (def statements) must be executed before they have any effect.......
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
4
by: Kurda Yon | last post by:
Hi, I start to learn the object oriented programing in Python. As far as I understood, every class has a set of corresponding methods and variables. For me it is easy to understand a method as a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.