0

In Python, to reduce clutter, is there a way to "Declare" members/methods of a class as "self" or a workaround.

class range:
 #range_start # Declare as self
 #range_end # Declare as self
 def __init__(self,my_range_start,my_range_end):
 range_start = my_range_start
 range_end = my_range_end
 def print_range(self):
 print ("%x %x" % (range_start,range_end))
asked Jun 4, 2015 at 5:38
9
  • you can use self.range_start and self.range_end...is that what your question is? Commented Jun 4, 2015 at 5:41
  • I don't want to use self. Commented Jun 4, 2015 at 5:41
  • 1
    @Jean, there's nothing to do with it. It is how python works. Commented Jun 4, 2015 at 5:42
  • There isn’t, and it doesn’t really seem like there’s a good reason to, either. Lots of complexity to save very few characters (and that’s not even counting this proposed declaration, which defeats the purpose [?] of saving... something). Commented Jun 4, 2015 at 5:43
  • @minitech what OP wants to do is stupid, but it's not like there is no way to do it. You can try dynamically injecting self variables into globals by declaring a descriptor for a name in a class block and do the magic by having the class use a specific metaclass to prepare the globals before calling the __get__ of methods. It's stupid and helps nothing, but it's not like it's impossible. Commented Jun 4, 2015 at 5:50

2 Answers 2

5

There isn't any straightforward way to do this. Also, if you did, you'd violate some rather deep set python conventions and your code would become much harder to read for other Python programmers. Too high a cost, in my opinion, to reduce visual clutter.

answered Jun 4, 2015 at 5:40
Sign up to request clarification or add additional context in comments.

3 Comments

Sure..But give that choice to the programmer.
Not in Python. The language tends toward "One and preferably only one way to do it". After working on a large perl code base for a few years, I've come to appreciate this. A set of language enforced rules in a large team makes the code much more readable and manageable for everyone.
Isn't this philosophy violated for re.match which does the same thing as re.search(r'^...') ?
2

You can not achieve what you ask for in the way that you want, the idea of implicit self has been discussed and discarded by the python community.

However, concerning your code example, you can reduce the amount of code:

class range(tuple):
 def print_range(self):
 print("%x %x" % self)

Still, that code is bad, because adding a print_range() function is the wrong approach (why is it called print_range() and not print(), btw?). Instead, provide implementations of __str__() and __repr__() to format the object.

Some more notes:

  • There is a namedtuple() function that creates a type similar to a tuple, which you should perhaps take a look at.
  • Calling the object self is a convention, you can name it anything you like. Others won't easily be able to read your code though.
  • Concerning style, check out PEP 8. There is a bunch of conventions that you should adhere to, unless you have a good reason not to.
answered Jun 4, 2015 at 5:51

3 Comments

Yep, PEP 8 covers that, it should be Range.
I think what he meant was about masking the keyword range.
range is not a keyword, and yes, I understood that part. One important point is that types should be UpperCase while functions like range() should be lower_case. Accidentally overwriting functions (id and list are classics there) could not have happened then. I believe that PEP 8 also talks about this issue.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.